2011-02-27 32 views
0

我移動舊服務器到archive.example.com,和新的服務器將繼續在example.com運行,而所有的WWW網址規範化要麼example.com或archive.example.com,並應處理尾隨削減的問題。重寫規則:重定向舊路徑歸檔服務器。規範化,以非www域名?

舊的服務器有很多的目錄,所以一切都需要重定向到archive.example.com同時保留的路徑信息,除了將在新服務器上運行的幾個目錄。我不想重定向,將繼續爲新服務器的目錄是:

 
/(root) 
/static 
/blog 
/about 

例如:

 
example.com => example.com 
www.example.com => example.com 
www.example.com/ => example.com/ 

example.com/blog => example.com/blog 
www.example.com/blog => example.com/blog 
www.example.com/blog/ => example.com/blog/ 

所有其他目錄應該重定向到archive.example.com。例如:

 
example.com/docs => archive.example.com/docs 
www.example.com/docs => archive.example.com/docs 
www.example.com/docs/ => archive.example.com/docs/ 

example.com/library/images => archive.example.com/library/images 
www.example.com/library/images => archive.example.com/library/images 
www.example.com/library/images/ => archive.example.com/library/images/ 

以下是我在我的httpd.conf文件:


ServerName example.com 
ServerAlias www.example.com 
UseCanonicalName On 

# canonicalize www.example.com to example.com 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule ^(.*)$ $1 [R=301] 

# redirect everything to archive.example.com except for a few directories 
RewriteCond %{REQUEST_URI} !^(/|/static|/blog|/about)$ 
RewriteRule ^/(.*)$ http://archive.example.com/$1 [NC,R=301,L] 

這是正確的和/或有更精確的方式?

回答

0

我相信我找到了我的問題 - 這是與重定向到舊網站的重寫規則。

這是我有什麼,當我張貼的問題:

 

# redirect everything to archive.example.com except for a few directories 
RewriteCond %{REQUEST_URI} !^(/|/static|/blog|/about)$ 
RewriteRule ^/(.*)$ http://archive.example.com/$1 [NC,R=301,L] 
 

...我改寫了這:

 

# redirect everything to archive.example.com except for a few directories 
RewriteCond %{REQUEST_URI} !^/$ 
RewriteCond %{REQUEST_URI} !^/static.*$ 
RewriteCond %{REQUEST_URI} !^/blog.*$ 
RewriteCond %{REQUEST_URI} !^/about.*$ 
RewriteRule ^(.*)$ http://archive.example.com%{REQUEST_URI} [NC,R=301,L] 
 

這裏的原因。

首先,你可以看到,我打破了單一的重寫條件分爲四個獨立的條件,因爲這將使我能夠清晰地排除增加更多的目錄作爲新網站的發展。

您還會注意到,我添加了一個點後明星/靜態/博客/和/約,這樣它會匹配任何路徑上在這些目錄中,而不僅僅是頂層。

最後,在重寫規則線I去除從圖案前導斜線和改變後行/ $ 1到%{REQUEST_URI}。我並不需要存儲從模式中的任何變數在這裏 - 我只需要改變服務器名稱 - 使用相同的%{REQUEST_URI}變量,而不是從圖形提取的路徑,我做了更明確的說在以前的四行使用。

順便說一句:這首先給我造成混亂的原因之一是,因爲Chrome有時會緩存DNS /路徑信息 - 執行Ctrl-F5清除緩存將使您能夠看到您的更改。

0
RewriteEngine On 
RewriteCond %{HTTP_HOST} !^gotactics.net$ [NC] 
RewriteRule ^(.*)$ http://gotactics.net/$1 [L,R=301] 

這將刪除所有www。我敢肯定,你可以改變,如果需要它也做不同的。