简体   繁体   中英

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]

Add this rule on top to remove trailing slash:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM