htaccess redirect specific domains to https

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:

  1. Redirects all domains to use 'www.'
  2. Checks if visitor is on www.domain1.com, www.domain2.com, or www.domain3.net
  3. Checks to see if HTTPS is NOT used.
  4. 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.