如何从.htaccess/mod重定向中的URL中删除一个或两个目录的尾部斜杠


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

我有以下URL:

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

需要自动转换为:

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

基本上,我希望删除尾部斜杠。

我现有的.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]

在顶部添加此规则以删除尾部斜线:

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