[英]Redirect http://www. and http:// to https for both root and subdomain
I have the following rules in my .htaccess: 我的.htaccess文件中有以下规则:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
This causes domains such as http://www.domain.com
to be redirected to https://domain.com
as well as http://www.sub.domain.com
to be redirected to https://sub.domain.com
. 这会导致将诸如
http://www.domain.com
域重定向到https://domain.com
以及将http://www.sub.domain.com
重定向到https://sub.domain.com
。
I would like to expand this rule to include redirection from http://domain.com
to https://domain.com
and http://sub.domain.com
to https://sub.domain.com
. 我想扩展此规则,以包括从
http://domain.com
到https://domain.com
重定向和从http://sub.domain.com
到https://sub.domain.com
重定向。
I've found a couple of examples online, but they don't seem to do quite what I would like: 我在网上找到了一些示例,但它们似乎并没有完全满足我的要求:
How to redirect http to https and https://www to https:// This example appears to hardcode domain and not support subdomain? 如何将http重定向到https和将https:// www重定向到https://此示例显示为硬编码域而不支持子域?
Redirect all http AND https non-www URLS to https://www.xyz.com via htaccess This is close, but is going to www not away from it which changes the regex. 通过htaccess将所有http和https非www URL重定向到https://www.xyz.com。这很近,但是要更改为正则表达式,请不要离开www。
I know this isn't a difficult change, but PHP/.htaccess files aren't my strongest suite. 我知道这不是一个困难的更改,但是PHP / .htaccess文件不是我最强大的套件。 Could someone enlighten me?
有人可以启发我吗?
Here is a rule that lets you do both redirects in one single rule: 这是一条规则,可让您在一个规则中同时执行两个重定向:
RewriteCond %{HTTP_HOST}::%{HTTPS} ^(?:www\.(.+?)::o(?:ff|n)|(?!www\.)(.+?)::off)$ [NC]
RewriteRule ^ https://%1%2%{REQUEST_URI} [R=302,L,NE]
It will do following redirections: 它将执行以下重定向:
http://domain.com => https://domain.com
http://www.domain.com => https://domain.com
https://www.domain.com => https://domain.com
http://www.sub.domain.com => https://sub.domain.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.