如何解决.htaccess强制HTTPS,在某些情况下还包括www但不包括特定的子域
我们在某些网站上使用了cPanel,我们使用以下.htaccess规则,并且在大多数情况下它们可以正常运行,但存在一些问题...
# Force HTTPS & NON-WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
使用以下规则,如果我们访问非www链接,则它们会正确切换到www。链接很好
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301]
</IfModule>
此Force HTTPS & WWW
规则的问题是当我们进入子域...
- 如果我们访问
staging.website.com
,则会错误地将其重写为www.staging.website.com
。当然可以使用类似RewriteCond %{HTTP_HOST} !^staging\. [NC]
的条件,但这意味着每个子域都需要一个设置。 - 添加该条件的另一个问题是,如果我们访问staging.website.com,将不会被迫使用https ...
- 在更复杂的设置中,我们有一个cPanel帐户,其中3个网站是
https://
,一个是https://www.
,对于那个特定的cPanel帐户,我不知道如何修改这些.htaccess规则,在两种情况下都可以工作...
在ipv6.website.com
上,#1和#2最引人注目
强制执行 httP和WWW
的规则测试-
https://website.com
->https://www.website.com
-
http://website.com
->https://www.website.com
-
https://www.website.com
->https://www.website.com
-
http://www.website.com
->https://www.website.com
-
https://ipv6.website.com
->https://ipv6.website.com
-
http://ipv6.website.com
->https://ipv6.website.com
-
https://www.ipv6.website.com
->https://ipv6.website.com
-
http://www.ipv6.website.com
->https://ipv6.website.com
-
https://randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
http://randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
https://www.randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
http://www.randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
https://ftp.website.com
->http://ftp.website.com
-
http://ftp.website.com
->http://ftp.website.com
-
https://www.ftp.website.com
->http://ftp.website.com
-
http://www.ftp.website.com
->http://ftp.website.com
强制 httP和非WWW的规则测试
-
https://website.com
->https://website.com
-
http://website.com
->https://website.com
-
https://www.website.com
->https://website.com
-
http://www.website.com
->https://website.com
-
https://ipv6.website.com
->https://ipv6.website.com
-
http://ipv6.website.com
->https://ipv6.website.com
-
https://www.ipv6.website.com
->https://ipv6.website.com
-
http://www.ipv6.website.com
->https://ipv6.website.com
-
https://randomsubdomain.website.com
->https://randomsubdomain.website.com
-
http://randomsubdomain.website.com
->https://randomsubdomain.website.com
-
https://randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
http://randomsubdomain.website.com
->https://www.randomsubdomain.website.com
-
https://ftp.website.com
->http://ftp.website.com
-
http://ftp.website.com
->http://ftp.website.com
-
https://www.ftp.website.com
->http://ftp.website.com
-
http://www.ftp.website.com
->http://ftp.website.com
解决方法
您似乎需要在此处设置2条单独的重定向规则:
- 要让根域添加
www
和https://
- 对于子域,仅添加
https://
您可以这样设置.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# enforce http:// and non-www for ftp domain:
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(ftp\..+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^ftp\. [NC]
RewriteRule ^ - [L]
# Force HTTPS & WWW for any subdomain not in list
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !(?:^|\.)(?:ipv6|mail|mobile|staging|cpanel)\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301]
# Force HTTPS & non-WWW for listed sub domains
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?:ipv6|maill|mobile|staging|cpanel)\..+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
</IfModule>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。