Created on 2016-Oct-07
Updated on 2016-Nov-20
Here's how to redirect ONLY specific domains to HTTPS with .htaccess.
RewriteEngine on # Redirect all domains to use 'www.' ​RewriteCond %{HTTP_HOST} . RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Redirect ONLY specific domains to HTTPS RewriteCond %{HTTP_HOST} ^(www\.)?(domain1.com|domain2.com|domain3.net)$ RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What the above does is:
- Redirects all domains to use 'www.'
- Checks if visitor is on www.domain1.com, www.domain2.com, or www.domain3.net
- Checks to see if HTTPS is NOT used.
- Redirects the above domains to use HTTPS
And that's that... The above should be pretty much minimum resource intensive, as we're first getting global tasks out the way first (change all domains to use 'www.'), followed by only processing criteria for specific domains and nothing else.
Rewrite ALL domains to use HTTPS
# Redirect to HTTPS RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The above will simply check to see if HTTPS is being used, IF NOT, then redirects to HTTPS.