2013-06-02 31 views

回答

13

爲了更具體地說明這種情況,您需要使用RewriteCond/RewriteRule而不是簡單的Redirect指令。對foo.mydomain.com做一個否定匹配(!)並執行重寫。您可以用OR組(foo|other1|other2)

RewriteEngine On 
# Redirect anything except foo.example.com, bar.example.com 
RewriteCond %{HTTP_HOST} !^(foo|bar)\.example\.com$ [NC] 
# Redirect to www.example.com, preserving the URI 
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302] 

匹配多個子域如果你只是想重定向到根,而不是通過$1追加整個URI,使用相同的RewriteCond,只是做:

# Match and redirect everything to the root of www.example.com 
RewriteRule ^. http://www.example.com/ [L,R=302] 
+0

謝謝非常! :) – Brett

+0

我不得不編輯'RewriteRule'到下面來使它工作:'RewriteRule ^。* http://www.example.com/ [L,R = 302]' – Brett