.htaccess forcing HTTPS

.htaccess, https, redirect

Solution

Your redirect happens whenever a request is made to the exact domain `mydomain.com` (that's what the RewriteCond is testing for). It doesn't apply to any other domains and doesn't detect HTTPS. Use this instead:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]

Problem

I am trying to force HTTPS on a domain. It must be done using a method that works by domain name and not port number (due to host structure/setup). My closest attempt was: ``` RewriteEngine On RewriteCond %{HTTP_HOST} ^mydomain.com$ RewriteRule ^.*$ https://www.mydomain.com/$1 [R=301,L] ``` This works when typing "mydomain.com" into the address bar, automatically redirecting to "https://mydomain.com" but when I type "www.mydomain.com" it does not work. I assume it is a syntax issue as I am very new to htaccess and have spent about 4 hours trying to create a solution from other's code. Any chance of a pointer? To make the setup a little more understandable. `/public_html/` - All files in this folder relate to `www.mydomain.com` `/public_html/subfolder` - These folders contain files also relating to `mydomain.com` `/public_html/subdomain` - These folders contain files relating to `www.myotherdomain.com` My other domains are subdomains of `mydomain.com` for to be listed in the cpanel on the host. For example: `subdomain.mydomain.com` is the same as `www.myotherdomain.com`. Hopefully that clears up the structure.

Original source