htaccess的力量HTTPS和WWW重定向到非www,但没有其他子域重定向、力量、htaccess、HTTPS

2023-09-02 00:51:35 作者:太阳暖人不暖心i

我知道有很多类似的主题,但没有一个似乎符合我确切的问题。下面是我想要做什么:

I know there are many similar threads, but none of them seems to match my exact problem. Here is what I’m trying to do:

(1) http://www.mydomain.com/ -> https://mydomain.com/
(2) http://mydomain.com/     -> https://mydomain.com/
(3) https://www.mydomain.com -> https://mydomain.com/

在理想情况下,我也想覆盖情况,如果我永远不会增加更多的子域,自动行为类似于以下(preferably在一个通用的方法,将工作的所有子域名我想补充)。

Ideally, I would also like to cover the situation if I will ever add more subdomains, to automatically behave like the following (preferably in a generic way that will work for any subdomain I add).

(4) http://sub.mydomain.com/ -> https://sub.mydomain.com/

在这一点上我想知道如果它甚至可能创造,做一切我需要一个单独的.htaccess文件,但我不得不承认,我的理解正则表达式,但我不完全是一个mod_rewrite的uberpro。

At this point I’m wondering if it is even possible to create a single .htaccess file that does everything I need, although I have to admit that I understand regexes, but I’m not exactly a mod_rewrite uberpro.

下面是我已经尝试过的解决方案:

Here are the solutions that I already tried:

强制非www和https通过htaccess的 排序的作品,但似乎产生重定向循环 工作为(1)和(3),但不适合(2) 重定向到 http://mydomain.com/ (无HTTPS)为(1) 在不执行任何操作的(2) 重定向到 http://mydomain.com/ (无HTTPS)进行(3) Redirects to http://mydomain.com/ (without https) for (1) Does nothing for (2) Redirects to http://mydomain.com/ (without https) for (3)

我的SSL证书涵盖了www子域,所以我不是在寻找一个不可信连接的错误的解决方案,我知道这是不可能的。

My SSL certificate covers the www-subdomain, so I’m not looking for a 'untrusted connection' error solution, I’m aware that isn’t possible.

推荐答案

将这个code在 DOCUMENT_ROOT /的.htaccess 文件:

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
 
精彩推荐
图片推荐