apache remove trailing slash

How to Remove Trailing Slash in Apache

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!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!