简体   繁体   English

重定向http:// www。 对于根域和子域,以及http://到https

[英]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.comhttps://domain.com重定向和从http://sub.domain.comhttps://sub.domain.com重定向。

I've found a couple of examples online, but they don't seem to do quite what I would like: 我在网上找到了一些示例,但它们似乎并没有完全满足我的要求:

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: 它将执行以下重定向:

  1. http://domain.com => https://domain.com
  2. http://www.domain.com => https://domain.com
  3. https://www.domain.com => https://domain.com
  4. http://www.sub.domain.com => https://sub.domain.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM