nginx- Rewrite URL with Trailing Slash
        Posted  
        
            by 
                Bryan
            
        on Server Fault
        
        See other posts from Server Fault
        
            or by Bryan
        
        
        
        Published on 2010-06-19T17:36:14Z
        Indexed on 
            2011/01/14
            6:55 UTC
        
        
        Read the original article
        Hit count: 351
        
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//
© Server Fault or respective owner