10

目前我有:

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

虽然我仍然希望这种重定向发生,但我不希望它重定向他们,如果他们去说foo.mydomain.com或这个子域上的任何其他页面。

我怎样才能做到这一点?

4

1 回答 1

22

要以这种方式更具体,您需要使用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]

如果您只想重定向到根目录而不是通过 附加整个 URI $1,请使用相同的方法RewriteCond并执行以下操作:

# Match and redirect everything to the root of www.example.com
RewriteRule ^. http://www.example.com/ [L,R=302]
于 2013-06-02T19:46:46.410 回答