0

所以基本上我希望每个询问 example.com/index.php 的人都能将 301 重定向到 example.com/,
每个询问任何 www 的人。将 301 重定向到相应的非 www 站点
(www.examle.com/foo -> example.com/foo)的 urls
并且每个请求站点的人都在 url 中使用双斜杠 [还有三重等] 斜杠以将 301
重定向到 url删除了双斜杠(example.com////foo -> example.com)。

如果有人应该对这三种情况进行任何组合,他仍然应该得到 301 重定向到正确的 url (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!

如何让它工作?

4

2 回答 2

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]
于 2012-08-19T20:49:03.433 回答
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,机器人只有一次......(没有循环)
www.example.com//foo -> example.com/foo -> example.com/foo

Also when www and any other condition is met, redirection happens in steps:
www.example.com/index.php -> example.com/index.php -> example.com/
www.example.com//index.php -> example.com/index.php -> example.com/

But that's not really big problem...

于 2012-08-20T08:12:17.753 回答