htaccess 将域重定向到 https,将子域重定向到 http,将 www 重定向到非 www
问题描述:
我正在尝试这样做:
为我的主域强制使用 https.
Force the https for my main domain.
http or https://www.domain.com -> https://domain.com
http or https://domain.com -> https://domain.com
但不适用于子域
http or https://www.subdomain.domain.com -> http://subdomain.domain.com
http or https://subdomain.domain.com -> http://subdomain.domain.com
并且总是删除 www.
And always removing the www.
现在我有了:
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
这会将 www 重定向到非 www 并将 http 重定向到 https,但不适用于子域.子域保留为 www 和 https.
This redirects www to non-www and http to https but not for subdomains. The subdomain remains with www and the https.
谢谢
答
您可以使用以下 2 条规则:
You can use these 2 rules:
# for main domain
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www.)?subdomain.domain.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]