.htaccess强制子域

时间:2018-01-15 13:40:25

标签: .htaccess ssl mod-rewrite https apache2

我已将主网站设置为使用https。我强制用户使用.htaccess进行https。我用这个代码

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

RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

但是我有一个子域名。这称为xxx。它的内容位于./xxx。如何强制xxx.domain.com仅使用http?

1 个答案:

答案 0 :(得分:1)

您可以使用此

RewriteEngine on 

#redirect www urls to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# do nothing if sub.domain.com is requested 
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^ - [L]
#redirect the main domain to https
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

在测试之前清除浏览器缓存。