3

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

domain.com/some-random-article to domain.com/some-random-article/

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;
            server_name  domain.com mirror.domain.com;
            root /rails_apps/master/public;
            passenger_enabled on;

 # Redirect from www to non-www
       if ($host = 'domain.com' ) {
         rewrite  ^/(.*)$  http://www.domain.com/$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 domain.com/some-random-article//

1 Answers1

4
rewrite ^(.*[^/])$ $1/ permanent; # Capture everything not with a trailing slash and add a trailing slash to it.
Martin Fjordvald
  • 7,589
  • 1
  • 28
  • 35