Laravel PHP Framework
Making Subdirectories Accessible without a Public folder
Technically there is some risk to this since it could potentially expose configuration files in the root directory. I negate this somewhat by adding a DENY rule to .env files. I still wouldn't recommend it for a production site, but I find it useful for development and preventing the need to create apache config files for every Laravel project.
- Create a Laravel project
laravel create newproject
- Rename the 'server.php' file to 'index.php' in the root directory
- Add a .htaccess file to the root directory:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(.*)/$ /hello/index.php/$1 [L]
</IfModule>
<Files .env>
Order Allow,Deny
Deny from all
</Files>
Note: Customize the RewriteRule line to match your subdirectory.
NGINX Setup
This configuration is for NGINX with PHP and FastCGI using FPM:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /laravel/laravel2 {
alias /usr/share/nginx/html/laravel/laravel2/public;
try_files $uri $uri/ @laravel2;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
location @laravel2 {
rewrite /laravel/laravel2/(.*)$ /laravel/laravel2/index.php?/$1 last;
}
location ~ [^/]\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
location /prscout {
alias /usr/share/nginx/html/prscout/;
try_files $uri $uri/ @prscout;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_read_timeout 300;
include fastcgi_params;
}
}
location @prscout {
rewrite /prscout/(.*)$ /prscout/index.php?/$1 last;
}
}