3

I am trying to force https and redirect non-www to www:

The problem is I have many sudomains and I don't have wildcard ssl (for now) so I want :

  • (x).domain.com (except www.domain.com) ===> http://*.domain.com

Is that possible ?

All the ansewers i found and tested only force https or redirect all non-www to www my problem is i don't want subdomains get https except for www and redirect non-www to www

web hobbit
  • 501
  • 4
  • 17
  • Possible duplicate of [htaccess force https and redirect www to non-www, but no other subdomains](http://stackoverflow.com/questions/21467329/htaccess-force-https-and-redirect-www-to-non-www-but-no-other-subdomains) – luchaninov May 10 '16 at 12:07

1 Answers1

5

To force https://www only for the www.example.com , you can use :

RewriteEngine on
#redirect http non-www to https://www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
#redirect https non-www to www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • thank you starkeen that what i was looking for, can you plz explain to me this line => RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ – web hobbit May 10 '16 at 13:21
  • 1
    This is condition for the rule, it says host value must start with **www.** or **example.com** the pattern `(www\.)?` matches "www." optionally. – Amit Verma May 10 '16 at 13:28