Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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//

share|improve this question

1 Answer 1

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

 
discard

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