removing multiple groups of slashes everywhere in URL in .htaccess

若如初见. 提交于 2019-12-08 10:09:00

问题


I currently have a website where guests are able to access each url with any number of slashes to separate folder names. For example, if a URL is supposed to be:

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

Then users could access the same page via any of the following:

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

However, I want the above example urls to only redirect users to this URL:

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

This is my .htaccess file to attempt to stop the enormous slashes:

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

The third line successfully stops trailing slashes on long URLs. The 4th and 5th lines are my attempt to stop trailing slashes right after the domain name, but that was unsuccessful.

The reason why I ask this question is because I don't want google to catch me for duplicate content and with adsense active on the site, google will likely scan all the URLs that I access.

Is there a RewriteCond/RewriteRule combo I can use to strip the middle slashes or is it more involved?


回答1:


You can use this rule for removing multiple slashes anywhere in URL:

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



回答2:


This works for me:

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]


来源:https://stackoverflow.com/questions/31933042/removing-multiple-groups-of-slashes-everywhere-in-url-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!