2013-06-02 9 views
6

とメインドメインではなく、特定のサブドメインをリダイレクト現在、私は持っている:は、.htaccessファイル

Redirect 302/http://www.example.com 

私はまだこのリダイレクトが起こることを望む一方で、彼らはfoo.mydomain.comまたはいずれかを言うために行けば、私はそれがそれらをリダイレクトする必要はありませんこのサブドメインの他のページ。

どうすればいいですか?

答えて

13

具体的には、Redirectというディレクティブではなく、RewriteCond/RewriteRuleを使用する必要があります。 foo.mydomain.comには(!)の否定一致を行い、書き換えを実行します。あなたは、あなただけの代わりに$1を経由して、全体のURIを追加するのルートにリダイレクトしたい場合は、同じRewriteCondを使用するか、またはグループ(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] 

で複数のサブドメインにマッチし、ちょうど行うことがあります。

# 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