How to add/remove the trailing slashes in URLs using the .htaccess?

You can use the below code in the .htaccess file depending on the trailing slash required or not -

No trailing slash
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

Trailing slash
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

Was this answer helpful?

 Print this Article

Also Read

Access Denied. Unable to establish a PHP session.

I was getting below error while accessing phpMyAdmin - Access Denied Unable to establish a PHP...

How to to know SSH port on the cPanel server?

Steps to know SSH port of cPanel server -1. Login to WHM with your login details. 2. Go to...

ERROR: There was an error connecting to the server, Please verify the settings

While updating the WordPress plugin using FTP client I was getting the error as -'ERROR: There...

The following errors were found : Drupal not upgrading using Softaculous

While upgrading the Drupal from 8.19 to 8.21 from cPanel >> Softaculous Application...

How to enable Open SSL on WHM?

Before enabling open SSL, you can check whether it is already enabled on your server. Please fire...

Powered by WHMCompleteSolution