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?