.htaccess规则强制https到根目录和特定子域

时间:2015-11-06 10:41:59

标签: regex apache .htaccess mod-rewrite

我想强制https到根目录和特定的子域只能从每个域中删除www。

以下条件会从所有域中删除www,并将所有域重定向到https。我需要https仅用于root和子域demo

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]

2 个答案:

答案 0 :(得分:1)

您需要在此处使用多个重写规则:

# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# if https is off
RewriteCond %{HTTPS} off
# a specific subdomain demo
RewriteCond %{HTTP_HOST} ^demo\.domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# if https is off
RewriteCond %{HTTPS} off
# main domain
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 1 :(得分:0)

现在看着我的规则说明了...... 如果http_host有www。 重写为没有www和https。

我认为您需要将规则分解为类似的规则。 如果http_host有www。 删除www 如果request_uri是根 添加https。

我认为你很接近,但我认为你需要多个规则而不仅仅是一个条件。

相关问题