2010-07-08 104 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