将所有子域重定向到除一些子域之外的主域

时间:2017-08-05 13:22:48

标签: .htaccess redirect

我在htaccess中使用此代码将所有子域重定向到主域

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

我想排除一些像test.example.com这样的子域名,所以我编辑了这样的代码

RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^test\.example\.com$ [NC] 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

但它不起作用,它将test.exmple.com重定向到主域example.com/test

那么如何编辑此代码以从重定向中排除测试

1 个答案:

答案 0 :(得分:2)

要匹配域名,您需要使用变量HTTP_HOST

RewriteCond %{HTTP_HOST} !^test\. [NC] 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,NE,L]