Remove Trailing Slash in NGINX

Last updated on August 22nd, 2024 at 04:22 am

Sometimes NGINX may show trailing slash in website URLs. This happens because the web server or your content management system such as WordPress have that setting enabled by default. It can be inconvenient to remember to add trailing slash after every URL. Also, if it is omitted, then your web server may give an error. Even search engines will display these slashes in their search results. Therefore, it is advisable to remove trailing slash in URL. Here’s how to remove trailing slash in NGINX to make your URLs look more intuitive.

Remove trailing slash in NGINX

Here are the steps to remove trailing slash in NGINX.

1. Open NGINX configuration file

Open terminal and run the following command to open NGINX server configuration file.

$ sudo vi /etc/nginx/nginx.conf

If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/website.conf then open its configuration with the following command

$ sudo vi /etc/nginx/sites-enabled/website.conf

Bonus Read : How to Install & Configure ModSecurity in NGINX

2. Remove trailing slash

Add the following rewrite rule in server block as shown in bold. Replace example.com below with your domain name

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

In the above code, the rewrite statement will redirect all URLs to those without trailing slash. URLs requested without trailing slash will work as it is.

If you want to remove trailing slash from only a specific URL (e.g /product/) then update the rewrite statement as shown below.

server { 
       listen 80; 
       server_name mydomain.com; 
       rewrite ^/product/$ /product permanent; 
}

Alternatively, you can add the earlier rewrite statement for the location block of the URL.

location /product { 
rewrite ^/(.*)/$ /$1 permanent;
}

Bonus Read : How to Enable NGINX Status Page

Remove Hash(#) from URL

Similarly, sometimes your site’s URLs may contain another unwanted character such as hash (#). You can use the same logic to remove hash marks from your site’s URLs. Replace slash with hash in the above configuration.

server { 
listen 80;
server_name example.com;
rewrite ^/(.*)#$ /$1 permanent;
}

OR

server {
listen 80;
server_name mydomain.com;
rewrite ^/product#$ /product permanent;
}

OR

location /product {
rewrite ^/(.*)#$ /$1 permanent;
}

3. Restart NGINX Server

Run the following command to check syntax of your updated config file.

$ sudo nginx -t

If there are no errors, run the following command to restart NGINX server.

$ sudo service nginx reload #debian/ubuntu
$ systemctl restart nginx #redhat/centos

Bonus Read : How to Install Varnish Cache in NGINX

Conclusion

It is always a best practice to remove trailing slashes from your website’s URLs as soon as you get your site up and running. It will ensure that even search engines capture the right URL in search results. And also users can easily remember your site’s URLs. Hopefully the above tutorial will help you remove trailing slash from URLs in NGINX. You can also use the above steps to remove other trailing characters such as hash (#) from URL.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!