在.htaccess中使用多次重写来删除重复的内容

时间:2012-08-19 15:04:09

标签: .htaccess url-rewriting

所以基本上我希望每个请求example.com/index.php的人都将301重定向到example.com/,
每个要求任何www的人。网址将301重定向到相应的非www网站
(www.examle.com/foo - > example.com/foo)
每个人都要求网站上的双重[也是三倍等]斜线在网址上获得301
重定向到带有双斜杠的url(example.com////foo - > example.com)。

如果有人应该对这三种情况进行任何组合,他仍然应该将301重定向到正确的网址(www.example.com////index.php - > example.com)。

所以我想到了:

RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ / [R=301,N] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*)$ /$1 [R=301]

但是,www.example.com//foo - > http://example.com/http:/example.com/foo = 404!

如何让它运作?

2 个答案:

答案 0 :(得分:0)

尝试以下几行:

RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]

RewriteRule ^index.php$ / [L,QSA,R=301]

RewriteRule ^(.*)/{2,}(.*)$ /$1/$2 [L,N,QSA,R=301]

答案 1 :(得分:0)

螺旋逻辑,认真......

我发现,.htaccess中的下载内容确实适当(小小的,有点怪癖)301重定向到正确的位置......

RewriteBase /
RewriteEngine on

...在顶部,重写规则本身:

RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]

RewriteRule ^index\.php$ / [QSA,R=301]

我发誓这些只是我的.htaccess中的重写规则。

怪癖是,www.example.com//foo重定向(301)到example.com/foo,然后example.com/foo再次重定向(301)到example.com/foo,bot只有一次。 ..(没有循环)
  www.example.com//foo - > example.com/foo - > example.com/foo

当满足www和任何其他条件时,重定向将按步骤发生:
  www.example.com/index.php - > example.com/index.php - > example.com/
  www.example.com//index.php - > example.com/index.php - > example.com /

但那不是什么大问题......