0


I would like to redirect my main domain to https://www.example.com (force www and https) and subdomains to https://subdomain.example.com (force non-www and https).
I would like to do this because my wildcard Let's Encrypt certificate (*.example.com) doesn't work for https://example.com. I have below .htaccess file:

RewriteEngine On 
# for subdomains: force ssl and non www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.*)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R=301,L]
# for main domains: force ssl and www
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L]

It doesn't redirect https://example.com to https://www.example.com
What's wrong with above file?

  • Possible duplicate of [htaccess redirect to https://www](https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www) – Walf May 17 '18 at 11:21
  • 1
    From what I can see, the only thing it fails to do is catch the case of https://www.subdomain.example.com you can't redirect https://example.com if the SSL certificate does not include it. In order to get redirected, the browser first must securely connect to the server which can never happen. – Walf May 17 '18 at 12:33
  • Thank you for the response. So it means that I have to generate a new certificate which includes example.com and *.example.com? – Szymon Zielonka May 19 '18 at 19:40
  • Well you don't say whether "doesn't redirect" means can't connect at all, or connects but domain doesn't change in address bar. If it's the former, then changing the certificate should fix it. If it's the latter, something else is wrong. – Walf May 20 '18 at 03:35

0 Answers0