Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a specialized set of rewrite rules to accommodate a mutli site cms setup. I am trying to have nginx force a trailing slash on the request URL. I would like it to redirect requests for to

I know there are semantic considerations with this, but I would like to do it for SEO purposes.

Here is my current server config.

    server {
            listen       80;
            root /rails_apps/master/public;
            passenger_enabled on;

 # Redirect from www to non-www
       if ($host = '' ) {
         rewrite  ^/(.*)$$1  permanent;

 location /assets/ {
 expires      1y;
   rewrite ^/assets/(.*)$ /assets/$http_host/$1 break;

 # / -> index.html
   if (-f $document_root/cache/$host$uri/index.html) {
     rewrite (.*) /cache/$host$1/index.html break;

 # /about -> /about.html
   if (-f $document_root/cache/$host$uri.html) {
     rewrite (.*) /cache/$host$1.html break;

 # other files
   if (-f $document_root/cache/$host$uri) {
     rewrite (.*) /cache/$host$1 break;


How would I modify this to add the trailing slash? I would assume there has to be a check for the slash so that you don't end up with

share|improve this question
rewrite ^(.*[^/])$ $1/ permanent; # Capture everything not with a trailing slash and add a trailing slash to it.
share|improve this answer
Does it matter where I place in relation to the other rewrites? – Bryan Jun 19 '10 at 19:36
Not really, it'll create a 301 redirect which causes a new HTTP request and thus will make Nginx re-evaluate the request. – Martin Fjordvald Jun 20 '10 at 12:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.