Gandalf
Gandalf

Reputation: 930

How to remove a trailing slash from one or two directories from URL in .htaccess / mod redirect?

I have the following URLs:

example.com/test/
example.com/test/mode/

that needs to be automatically converted to:

example.com/test
example.com/test/mode

Basically, I want the trailing slash to be removed.

My existing .htaccess

RewriteEngine On

# Rule to handle example.com/xyz/mode
RewriteRule ^(\w+)/(\w+)/?$ /index.php?id=$1&mode=$2 [L,QSA]

# Rule to handle example.com/xyz
RewriteRule ^(\w+)/?$ /index.php?id=$1 [L,QSA]

Upvotes: 2

Views: 113

Answers (1)

anubhava
anubhava

Reputation: 785611

Add this rule on top to remove trailing slash:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=302,L]

Upvotes: 1

Related Questions