Reputation: 73
I have changed hole webstore to https. So I want to rewrite all domains except mobile subdomain (http://m.my-store.com) to https://www.my-store.com
#First rewrite any request to the wrong domain to use the correct one (here www.)
#mobile subdomain shouldn't rewrite
RewriteCond %{HTTP_HOST} !m\.
RewriteCond %{HTTP_HOST} !^www\.my-store\.com$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.my-store\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Result:
http://my-store.com OK (correct rewrite to https://www.my-store.com)
http://www.my-store.com OK (correct rewrite to https://www.my-store.com)
https://my-store.com X (stays with https://my-store.com)
https://www.my-store.com OK (correct rewrite to https://www.my-store.com)
http://m.my-store.com OK (correct rewrite to https://www.my-store.com)
Upvotes: 0
Views: 138
Reputation: 24478
What I would do is just check the domain instead of trying to match if www
is not in the request. Instead do the opposite and just check for the base domain and that means there is no www, so redirect. You can also leverage using [OR] and this one rule and it will take care of all scenarios.
You this rule below.
RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ https://www.my-store.com/$1 [L,R=301]
Don't do it like this below.
I used the actual domain in the rewriterule above so that you wont have issues with the variables because you will using this method with OR
. Meaning if you have your rules like this.
RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And your request http://www.my-store.com
you will end up with https://www.www.my-store.com
So use the first rule I provided.
Your result will be using the first rule above.
http://my-store.com (rewrites to https://www.my-store.com)
http://www.my-store.com (rewrites to https://www.my-store.com)
https://my-store.com (rewrites to https://www.my-store.com)
https://www.my-store.com (does nothing - https://www.my-store.com)
http://m.my-store.com (does nothing - http://m.my-store.com)
Upvotes: 1