在.htaccess中删除URL中的多个斜杠组

问题描述 投票:0回答:2

我目前有一个网站,客人可以使用任意数量的斜杠访问每个网址,以分隔文件夹名称。例如,如果URL应该是:

http://example.com/one/two/three/four

然后,用户可以通过以下任何一种方式访问​​同一页面:

http://example.com/one//two///three////four/////
http://example.com/one/two////three/four/////
http://example.com///one///////////two////three/four/
http://example.com///////////one///////////two/three/four

但是,我希望上面的示例网址仅将用户重定向到此网址:

http://example.com/one/two/three/four

这是我的.htaccess文件,试图阻止巨大的斜线:

RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteRule ^(.*)/+$ /$1 [R=301,L,NC]
RewriteCond %{REQUEST_URI} ^/+(.*)/+$
RewriteRule .* /%1 [R=301,L]

第三行成功停止长URL的尾部斜杠。第4行和第5行是我尝试在域名后立即停止斜杠,但这是不成功的。

我问这个问题的原因是因为我不希望谷歌抓住我的重复内容,并且在网站上有活跃的adsense,谷歌可能会扫描我访问的所有网址。

有没有RewriteCond / RewriteRule组合我可以用来剥去中间斜线或者它更复杂?

apache .htaccess mod-rewrite slash trailing-slash
2个回答
10
投票

您可以使用此规则删除URL中任何位置的多个斜杠:

RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ $0 [R=302,L,NE]

0
投票

这对我有用:

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.