2010-07-08 106 views
1

我一直在试图管理从www.domain.com到domain.com, 的重定向,但似乎没有任何效果。 我总是得到一个重定向循环 - 我尝试了各种在Google或Google上找到的东西。htaccess无重定向问题www

所以这里是我的.htaccess,也许有人可以帮我弄清楚我能做些什么来正确重定向,或者如果在这里有什么问题。

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] 

# Redirect all to .php 
# Example: example.com/hello -> example.com/hello.php 
RewriteRule ^(.*)$ $1.php [L,R=301] 


# show example.com/index.php always as example.com/ 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ http://example.com/ [R=301,L] 

非常感谢! 我已经花了很多时间试图弄清楚这一点。

回答

1

你有一个总是匹配的规则,它负责无限重定向。我已经更新了您的规则集以解决该问题并执行您在答案顶部提到的重定向。让我知道这是否符合你的期望。

RewriteEngine On 

# Redirect www.example.com to example.com 
RewriteCond %{HTTP_HOST} ^www [NC] 
RewriteRule ^.*$ http://example.com/$0 [R=301,L] 

# This performs an external redirection? Is that what you want? 
# Don't do the rewrite if we're already pointing at a file, otherwise we'll 
# just redirect over and over because .* matches what we redirect to, too 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI}  !\.php$ 
RewriteRule ^.+$ $0.php [L,R=301] 

# show example.com/index.php always as example.com/ 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ http://example.com/ [R=301,L] 
+0

非常感谢! 随着你的第二个RewriteCond&RewriteRule,它再次给了我循环,但如果我把它放开,它就可以工作。 谢谢! – rafleo 2010-07-08 09:33:09

+1

没问题。如果你需要第二部分不工作(对此感到抱歉),我继续进行更新,以便处理那些也会导致问题的边缘情况(尽管我没有完全清醒,所以可能会有更多的我没有考虑)。 – 2010-07-08 09:36:40

0

答案是Apache documentation,文档说明如何强制使用www。你只需要扭转这个例子。

RewriteCond %{HTTP_HOST} !^example\.com [NC] 
RewriteCond %{HTTP_HOST} !^$ 
RewriteRule ^/(.*)   http://example.com/$1 [L,R] 
+0

恐怕这会给我一个内部服务器错误(500) – rafleo 2010-07-08 09:29:04