How to remove the trailing slash from a URL using Apache or Nginx

This tutorial explains how to rewrite urls having a trailing slash to an url without the trailing slash.

Apache

Using Apache we have two options, set the rewrites in a .htaccess (slower) file or in the vhost configuration.

.htaccess

Add the highlighted lines to your .htaccess file, keep in mind that the position is important. I advice you to keep it in the same order as below.

vhost configuration

In a vhost configuration it works almost identical as the .htaccess file, same rule as above, keep the order correctly to make it function.

 

Nginx

In Nginx we have one type of configuring, in the vhost config. This is the webserver I prefer most.

Add the highlighted lines to your server block to rewrite the urls.