2011-04-17 186 views
2

我正在嘗試將www和非www重定向到非www子文件夾/論壇。htaccess 301將www和非www重定向到非www子文件夾

我目前使用此代碼:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR] 
RewriteCond %{REQUEST_URI} ^/$ 
RewriteRule ^(.*)$ http://example.com/forums/$1 [R=301,L] 

它運作良好,但也存在一些問題,與此有關。

如果我輸入URL:example.com,它會將我帶到example.com/forums。 -OK

如果我輸入URL:example.com/test,它會將我帶到example.com/test。 -OK

如果我輸入URL:www.example.com,它會將我帶到example.com/forums。 -OK

如果我輸入URL:www.example.com/test,它會將我帶到example.com/forums/test。 -WRONG

如何解決www.example.com/test發生的錯誤?

我想要www.example.com/test導致example.com/test。

請幫助我,謝謝!

+0

幫我這麼多謝謝! – Jake 2011-12-30 22:53:19

回答

2

嘗試按照你的.htaccess文件規則:

RewriteEngine on 
Options +FollowSymlinks -MultiViews 

# redirect empty URL to /forums 
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC] 
RewriteRule ^$ http://example.com/forums [R=301,L] 

# non forums handler 
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/forums [NC] 
RewriteRule ^(.+)$ http://example.com/$1 [R=301,L] 
+0

謝謝,它的工作! – initialshl 2011-04-18 11:29:32

0

如果你沒有在同一目錄指向(或者也沒關係,他們將被重定向太)任何其他子域,那麼你可能只是試試這個:

RewriteEngine On 
RewriteRule ^$ /forums/ [R=301,L] 

這重定向任何請求到www.example.com/和example.com/到子文件夾/論壇/,並保持所有其他請求不變。

假設您提供的測試用例是唯一需要考慮的測試用例,那麼這應該可以工作。如果您有任何其他情況,則可能需要擴展規則。

+0

謝謝,但沒有很好的工作。我輸入www.example.com/test,它給了我www.example.com/test。我需要它是example.com/test。 – initialshl 2011-04-17 14:13:12

+0

好的,在編輯之前並不明顯。我建議嘗試其他給定的答案,看起來相當不錯。 – jCoder 2011-04-18 05:47:52