Nginx remove trailing slash

Written by
Date: 2011-07-30 21:04:00 00:00


For good SEO you better have only one url for every post or article in your site, as having two uris responding with the same content may lead to duplicate content and that is penalized by the search engines, specially by Google which is the biggest by far.

So, if you are using Nginx and want to remove the trailing slash from all URIs in your site, so if someone types:

http://yoursite.com/ will be automatically redirected to http://yoursite.com

Here is the code you need to add to your nginx.conf file.

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

That goes in the server section so something like this:

server {
listen  :80;
server_name  www.site.com;
rewrite ^/(.*)/$ /$1 permanent;
}

You are done, now, Nginx will take care about those trailing slashes.