htaccess http to https with www. Without redirecti

2019-09-05 00:55发布

I have a RewriteRule that redirects my main domain to https://www.sta-games.com which works fine, but when I try to access my subdomain http://files.sta-games.com, it redirects to my main domain.

Heres my redirect rules

#HTTPS Redirection
RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Can anyone see the problem?

2条回答
Fickle 薄情
2楼-- · 2019-09-05 01:38

You need to adjust your rules so that it looks for the whole domain instead of just part of it. Right now you are only looking for the absence of www. So that is why your subdomain redirects.

First you need to combine your rules because it seems you want to redirect if https is not on and there is no www, so make that one rule. Then use your actual domain name in the rule. Replace example.com with your domain. This should fix your issue.

#HTTPS Redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
查看更多
劫难
3楼-- · 2019-09-05 01:43

Have an additional skip condition for all domains except the main domain:

RewriteCond %{HTTP_HOST} ^(www\.)?sta-games\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.sta-games.com%{REQUEST_URI} [L,R=301,NE]

Test this after clearing your browser cache.

查看更多
登录 后发表回答