删除.htaccess中URL各处各处的多组斜杠 [英] removing multiple groups of slashes everywhere in URL in .htaccess

查看:194
本文介绍了删除.htaccess中URL各处各处的多组斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

这是我的.htaccess文件,旨在阻止出现大量斜线:

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]

第三行成功停止了长URL上的尾部斜杠.第4行和第5行是我尝试在域名后立即停止斜杠的方式,但这没有成功.

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.

之所以问这个问题,是因为我不希望Google捕获重复的内容,并且网站上的adsense处于活动状态,google可能会扫描我访问的所有URL.

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.

是否可以使用RewriteCond/RewriteRule组合剥离中间的斜杠?或者它是否涉及更多?

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

推荐答案

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

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

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

这篇关于删除.htaccess中URL各处各处的多组斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆