2

I’m trying to do that:

Force the https for my main domain.

http or https://www.domain.com  -> https://domain.com
http or https://domain.com  -> https://domain.com

But not for subdomains

http or https://www.subdomain.domain.com -> http://subdomain.domain.com
http or https://subdomain.domain.com -> http://subdomain.domain.com

And always removing the www.

Now i have that:

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

This redirects www to non-www and http to https but not for subdomains. The subdomain remains with www and the https.

Thanks

Devilquest
  • 102
  • 3
  • 3
  • 9
  • 1
    What do you mean by "none work for my situation"? What you want to do is nothing special, there are existing solutions. Please post your attempts and explain why they did not work for you. That way we can work out together what has to be fixed in your tries. – arkascha Mar 16 '15 at 22:46
  • You also wanted to "explain why they did not work for you". What does it do, what does it not do? – arkascha Mar 16 '15 at 23:37
  • They work for domains but not for subdomains. – Devilquest Mar 16 '15 at 23:47

3 Answers3

10

You can use these 2 rules:

# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]

# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

In order to get the combination you want. You will need to use your domains.

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com [NC]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://sub.example.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^sub\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
0

I also used the following and it worked great. I was having a hard time pointing my primary domains www.domain.com and http://domain.com to https://domain.com

Thanks very much; as I've been trying to get this done for hours now!!!

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
Jonni
  • 1