How to Remove Trailing Slash in Apache

Last updated on September 23rd, 2020 at 05:21 am

By default, Apache and WordPress append trailing slash to all its URLs. Here’s how to remove trailing slash in Apache and WordPress.


How to Remove Trailing Slash in Apache

Before proceeding further, you need to enable mod_rewrite in Apache web server. Here are the steps to enable mod_rewrite (.htaccess) in Apache web server.

1. Open htaccess file

Open terminal and run the following command to open .htaccess file. Please change the file path to .htaccess file as per your requirement.

$ sudo vi /var/www/html/.htaccess

Bonus Read : How to Disable mod_security in Apache


2. Remove trailing slash

Add the following 2 lines to redirect all URLs with trailing slash to URLs without trailing slash.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

In the above code, the first line checks that the request is a URL and not a directory. The second line redirect matching URLs with trailing slash to those without trailing slash.

Alternatively, you can add the following line

RewriteRule ^(.*)/+$ $1 [R=301,L]

Bonus Read : How to Prevent SEO Bots from Your Site


3. Restart Apache web server

Restart Apache server with following command

$ sudo systemctl restart httpd

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