主域的https和所有子域的http

问题描述 投票:1回答:1

我使用此代码但不起作用

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
.htaccess
1个回答
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。

© www.soinside.com 2019 - 2024. All rights reserved.