重定向整个网站到HTTPS - 非www到www除了子域重定向、网站、www、HTTPS

2023-09-02 01:02:08 作者:爱莪迩怕了吗

我需要设置我的.htaccess执行以下重定向我的网站。 (我花了相当长的一段时间,所以研究和发现大量的相关htaccess的重定向问题,但不是这个特定的一个。)

I need to setup my .htaccess to perform the following redirects for my website. (I have spent quite some time researching SO and found lots of questions related to htaccess redirect but not this specific one.)

 1. www.example.com
 2. http://www.example.com
 3. example.com
 4. http://example.com
 5. https://example.com


to https://www.example.com

然而,任何的子域不应被重定向到www或https  即 support.example.com

However any subdomains should NOT be redirected to www or https i.e. support.example.com

不应该被重定向到 https://support.example.com www.support.example.com

目前,我有以下重定向code在我的.htaccess

I currently have the following redirect code in my .htaccess

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{HTTPS} !=on 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

这正确重定向

 1. www.example.com
 2. http://www.example.com

to https://www.example.com

和也$ P $的重定向pvents的子域。

and also prevents the subdomains from redirecting.

然而

 3. example.com
 4. http://example.com
 5. https://example.com

将被重定向到

are redirected to

 https://example.com

P.S。我已经把里面的code块的链接,因为我没有足够的声誉后2个以上环节。

P.S. I have put the links inside code-blocks because I dont have enough reputation to post more than 2 links.

推荐答案

试试这个,看看它是如何工作的你,让我知道。

Give this a try and see how it works for you and let me know.

RewriteCond %{HTTP_HOST} ^(example.com) [NC]
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]

RewriteCond %{HTTPS} !^on$
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]