How to Remove Trailing Slash in NGINX

remove trailing slash in nginx

By default, NGINX adds trailing slash to its URLs. So if your website visitors don’t add a trailing slash to their requested URLs, they will get a “404 : Page not found” error. Also, search engines will see these 2 URLs (with and without slash) separately and treat them as duplicate content. So it is advisable to remove trailing slash from your URLs. Here’s how to remove trailing slash in NGINX.


How to Remove Trailing Slash in NGINX

Here are the steps to remove trailing slash in NGINX. It’s quite easy really.


1. Open NGINX config file

Open NGINX config file using a text editor. By default, NGINX error log is located at log/error.log. You will also find it at /var/log/nginx/ on some Linux distributions.

$ sudo vim /var/log/nginx/nginx.conf


2. Update NGINX config file

Place the following line in the server block on your config file

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


Here’s a complete example:

server {
 listen 80;
 rewrite ^/(.*)/$ /$1 permanent;


3. Reload NGINX web server

Reload NGINX web server to apply changes

$ sudo service nginx reload


That’s it! Now all URLs with slashes will be redirected to the ones without slash. So search engines will treat both versions (with and without slash) as same pages. Also, users will be sent to the correct web page irrespective of whether or not they use a slash in the requested URL.



About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!