Add slash to the end of every url (need rewrite rule for nginx)

More likely I think you would want something like this:

rewrite ^([^.]*[^/])$ $1/ permanent;

The Regular Expression translates to:
“rewrite all URIs without any ‘.’ in them that don’t end with a “https://stackoverflow.com/” to the URI + “https://stackoverflow.com/””
Or simply:
“If the URI doesn’t have a period and does not end with a slash, add a slash to the end”

The reason for only rewriting URI’s without dots in them makes it so any file with a file extension doesn’t get rewritten. For example your images, css, javascript, etc and prevent possible redirect loops if using some php framework that does its own rewrites also

Another common rewrite to accompany this would be:

rewrite ^([^.]*)$ /index.php;

This very simply rewrites all URI’s that don’t have periods in them to your index.php (or whatever file you would execute your controller from).

Leave a Comment