4

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.

1 Answer 1

3

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]
7
  • I am using Bluehost as my host. Their set up method is that the primary site (the one I am trying to use https with) is listed under "public_html". All my other sites are in subfolders of "public_html" and are affected by the .htaccess file, causing all domains to now redirect to "mydomain.com"
    – Daniel
    Commented Jun 14, 2012 at 20:17
  • @Daniel I see. I will update my answer. Are any of your other sites subdomains of mydomain, or all they all different domains?
    – Paul
    Commented Jun 14, 2012 at 22:09
  • I have a domain name for each, but for them to be on the cpanel, they must be assigned subdomains. For example: www.mydomain.com and subdomains.mydomain.com (which is also www.myotherdomains.com)
    – Daniel
    Commented Jun 14, 2012 at 22:35
  • @Daniel And you only want to force HTTPS on www.mydomain.com and mydomain.com, right?
    – Paul
    Commented Jun 14, 2012 at 22:48
  • correct. I have updated original post with layout, to clear it up a little. Cheers.
    – Daniel
    Commented Jun 14, 2012 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.