Skip to content

Instantly share code, notes, and snippets.

@josephspurrier
Last active Jun 1, 2017
Embed
What would you like to do?
Nginx root configuration for Trailing Slash Solution - http://josephspurrier.com/trailing-slash-solution/
http {
# Ensure index.php is only allowed as index
index index.php;
server {
listen 80;
server_name localhost;
root /var/www;
# Manage the slashes via rewrites
merge_slashes off;
# Strip main index.php and query string
if ( $request_uri ~* ^/+index\.php\?+) {
return 301 /;
}
# Strip multiple slashes and query string
rewrite "^/+(.*[^/])/{2,}(.*)" /$1/$2 permanent;
# Strip multiple leading slashes and query string
rewrite "^/{2,}(.*)" /$1? permanent;
# Add a trailing slash to folders like Apache
if (-d $request_filename) {
rewrite /(.*[^/])$ /$1/? permanent;
}
location @rewrites {
# Send all requests to index.php
rewrite ^ /index.php last;
}
location ~* \.php$ {
try_files $uri $uri/ @rewrites;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri/ @rewrites;
}
}
}
@josephspurrier
Copy link
Author

josephspurrier commented Mar 29, 2014

A sub folder Nginx config file is available here: https://gist.github.com/josephspurrier/9848964.

By using these two scripts, both applications will handle pages the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment