如何从重定向中排除子域名?

时间:2016-03-22 15:14:14

标签: regex linux apache .htaccess mod-rewrite

我有这段代码强制任何网址为" https://www。":

#rewrite to WWW:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTP_HOST} !^(\d+)
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我有子域名,我不希望它们中的任何一个被重定向。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以在单一规则中完成所有操作:

RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(?:static\.|subdomain1\.|subdomain2\.|\d+) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

确保在测试此规则之前清除浏览器缓存。