0

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)
Astarot
  • 61
  • 1
  • 8
  • 1
    _`http://m.my-store.com OK (correct rewrite to https://www.my-store.com)`_ <- isn't this wrong? – hjpotter92 Oct 21 '15 at 10:41
  • sorry, copy&pase mistake. It should be http://m.my-store.com OK (stays with http://m.my-store.com) – Astarot Oct 21 '15 at 10:45
  • `https://my-store.com` stays at same URL, isn't this what you want? – anubhava Oct 21 '15 at 11:52
  • no, I want to redirect the URL `(https://my-store.com)` to the URL `(https://www.my-store.com)` – Astarot Oct 21 '15 at 13:19
  • Since you only have %{HTTP_HOST} as the target it will not add www to the URL in your 3rd example. You may have to make two groups for adding https (one for m.my-store.com and one for the rest) – Lars Lind Nilsson Oct 21 '15 at 14:23
  • `http://m.my-store.com` shouldn't be changed. So subdomain works fine without `https`. I want just for `https://my-store.com` a redirect to `https://www.my-store.com` with www. So from `https://` to `https://www.` This is not working. – Astarot Oct 21 '15 at 14:55

1 Answers1

1

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)
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • I've used your rules `RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^m\. RewriteRule ^(.*)$ https://www.my-store.com/$1 [L,R=301]` but result is the same as with my rules. Your variant is better, because it has less lines ;) But `https://my-store.com` has no redirect to `https://www.my-store.com` – Astarot Oct 21 '15 at 16:36
  • `https://my-store.com (rewrites to https://www.my-store.com)` doesn't work :( – Astarot Oct 21 '15 at 16:41
  • If you used my first rule it does. Clear your browser cache and try again. – Panama Jack Oct 21 '15 at 16:43
  • Or try in another browser to make sure there's no cache – Panama Jack Oct 21 '15 at 16:48
  • http://web-sniffer.net/ says `Status: HTTP/1.1 301 Moved Permanently` it looks correct, but my browser tells me bad ssl certificate. – Astarot Oct 21 '15 at 16:50
  • I have tried it in Internet Explorer ;) It doesn't work. See yourself `https://koeder-laden.de` – Astarot Oct 21 '15 at 17:02
  • 1
    Ok it has nothing to do with the rules I provided. It's not working because you have an invalid certificate. You can't redirect if the cert is not valid first. Please see this answer to explain why it won't work for you. You need a valid cert for `koeder-laden.de` http://stackoverflow.com/questions/33039666/www-to-non-www-urls-remove-www-using-apache-htaccess/33045563#33045563 – Panama Jack Oct 21 '15 at 17:30