2013-02-24 109 views
4

我期待在舊域名重定向每一個網頁頁面上的新領域。它也應該重定向www和非www版本。對域old.com的每一個請求,無論它是什麼,都應該導致www.new.com根。的.htaccess 301重定向到一個頁面上舊域中的所有網頁上的新域名

old.com/1.php 
www.old.com/2.php 
old.com/2.php 
old.com/links/index.php 
old.com/about-us.html 
old.com/?string=1 

都應該重定向到:

www.new.com/

我正在尋找一個解決方案的.htaccess。

回答

14

您可以在old.com虛擬主機

RedirectMatch permanent .* http://www.new.com/ 

Redirect使用RedirectMatch附加匹配的URI來重定向尾部URI但RedirectMatch它是由你來選擇部分(如果有的話)你想使用圓括號和$number引用進行傳送。

如果舊的和新的域必須是磁盤上的同一個文檔根目錄的別名,那麼你可能需要使用mod_rewrite

RewriteEngine on 
RewriteCond %{HTTP_HOST} old\.com$ 
RewriteRule .* http://www.new.com/ [L,R=301] 
+0

作品

在.htaccess文件將這個對於大多數網頁,但不適用於www.old.com/?string=1 – 2013-02-24 22:11:26

+0

第二種解決方案也不會重定向www.old.com/?string=1(與www.old.com/index.php相當?字符串= 1)。它確實重定向了所有以文件夾形式存在的URL(www.old.com/folder/) – 2013-02-24 22:28:31

+3

@DanHorvat如果您向新域添加問號'?',查詢字符串將被移除'RewriteRule。* http ://www.new.com/? [L,R = 301]' – 2013-02-24 22:52:00

0

這將處理所有的重定向,包括查詢字符串。請問格式化,我

RewriteCond %{HTTP_HOST} old\.com$ RewriteCond %{QUERY_STRING} .+ 
RewriteRule (.*) http://www.new.com$1?%{QUERY_STRING} [L,R=301,QSA] 

RewriteCond %{HTTP_HOST} old\.com$ 
RewriteRule (.*) http://www.new.com$1 [L,R=301,QSA] 
4

這已經回答了電話:)上,但這裏一共包含了有益的意見使人們有一個參考回首後來他們應該忘記它的代碼。

## Each and every page on old domain redirects to single page 
## By appending question mark to new domain, query strings are removed 
RewriteEngine on 
RewriteCond %{HTTP_HOST} old\.com$ 
RewriteRule .* http://www.new.com/? [L,R=301] 

您也可以重定向到其他域上的子目錄,因爲是我需要做的是這樣的情況:

## Each and every page on old domain redirects to single page 
## By appending question mark to new domain, query strings are removed 
RewriteEngine on 
RewriteCond %{HTTP_HOST} old\.com$ 
RewriteRule .* http://www.new.com/sub-directory/? [L,R=301] 
相關問題