主域名为https,所有子域名为http

时间:2019-03-26 10:14:02

标签: .htaccess

我使用此代码但不起作用

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

RewriteCond %{HTTP_HOST} ^(.+)\.blahblah\.com
RewriteCond %{HTTP_HOST} !^(www.)\.blahblah\.com
RewriteRule (.*) http://blahblah.com/shop.php?id=$1 [P,L]

我希望所有域www或非www重定向到https://blahblah.com

所有子域都重定向到http:

http://b.blahblah.com
http://c.blahblah.com

1 个答案:

答案 0 :(得分:0)

应遵循以下规则:

RewriteEngine on

#enforce https for main domain
RewriteCond %{HTTP_HOST} ^maindomain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
#enforce http for all subdomains of maindomain
RewriteCond %{HTTP_HOST} ^(.+)\.maindomain\.com$ [NC]
RewriteCond %{HTTPS} on [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

第一个规则块将301 maindomain.com重定向到https版本。 如果使用https方案访问所有子域,第二条规则会将所有子域重定向到http。