简体   繁体   English

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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