How to add a trailing slash on URLs in Nginx


TL;DR for how to add a trailing slash on urls in nginx:

rewrite ^([^.]*[^/])$ $1/ permanent;
Specification:

OS Version: Ubuntu Os 18.04.3 (LTS) x64
Nginx version: nginx/1.14.0

Full read below!

When you reconfigure Nginx for a very custom use case it happens that you also need to manage how URLs are being handled. Whether it’s for honoring old URLs that are shared all over the Internet. Or social networks or just because you have a custom code that requires old URLs to be accessible. These old URLs are, let say, WITH trailing slash whereas your new custom setup of Nginx has no trailing slashes.

We’ll use the URL we’ve used in some other posts and use it as a testing page /security-in-remote-working-culture. Below is the code sample of the location block that will capture the request for this page without slash and it will redirect us to the same page with trailing slash:

location ~ /security-in-remote-working-culture {
                proxy_cache my_zone;
                proxy_pass http://bluegrid.io;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
 
                rewrite ^([^.]*[^/])$ $1/ permanent;
        }

Let’s check it out! Oh, note that for this purpose we’ve used the curl command with flag L. We want to follow every redirection Nginx gives us:

bluegrid-edu:~# curl -I http://bluegrid.io/security-in-remote-working-culture -L
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:35:16 GMT
Content-Type: text/html
Content-Length: 194
Location: http://bluegrid.io/security-in-remote-working-culture/
Connection: keep-alive
 
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:35:16 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.38
Last-Modified: Sun, 26 Jul 2020 14:13:53 GMT
ETag: "93f69114682172bc71948b568fc1ef7a"
Vary: Accept-Encoding
Referrer-Policy: no-referrer-when-downgrade
Cache: HIT

That’s it!

What if we need to remove trailing slash on urls in Nginx?

What if we actually need to remove the trailing slash for some reason? In that case, we’d be doing something like this:

location ~ /security-in-remote-working-culture/ {
                proxy_cache my_zone;
                proxy_pass http://bluegrid.io;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
 
                rewrite ^/(.*)/$ /$1 permanent;
        }

Now, we are matching everything that starts with the “/” at the beginning of the URi and ends before the last “/”. Using “$” in regex we match the end of the segment. Then we are moving this matched string and redirecting to it without a slash at the end:

bluegrid-edu:~# curl -I http://bluegrid.io/security-in-remote-working-culture/ -L
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:46:07 GMT
Content-Type: text/html
Content-Length: 194
Location: http://bluegrid.io/security-in-remote-working-culture
Connection: keep-alive
 
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 26 Jul 2020 14:46:07 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.38
Last-Modified: Sun, 26 Jul 2020 14:46:06 GMT
ETag: "93f69114682172bc71948b568fc1ef7a"
Vary: Accept-Encoding
Referrer-Policy: no-referrer-when-downgrade
Cache: HIT
Share this post

Share this link via

Or copy link