Home .htaccess - force www and https for main domain and non-www and https for subdomains
Reply: 0

.htaccess - force www and https for main domain and non-www and https for subdomains

user894
1#
user894 Published in June 21, 2018, 12:57 am


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?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312565 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO