I recently wanted to setup SSL on 2 of my websites. So I purchased the SSL Certificates and performed the installation through my host’s domain portal.
My host modified my default .htaccess file by adding
#RewriteEngine On
#RewriteCond %{SERVER_PORT} 80
#RewriteRule ^(.*)$ https://DomainName.com/$1 [R=301,L]
The problem was that I had a subdomain, for personal use, on one of my domains and I did not want to force https on it since my SSL certificate was only for the main domain. The above ReWrite rule is indiscriminant and forces https on all urls. I contacted my host and they weren’t very helpful, so I knew I was on my own to figure this one out.
I won’t bore you with all the variations I tried (hours of Googling and trying this and that), but below was what finally worked for me to exclude a subdomain from being forced into https.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
#Exclude subdomain from rewrite rule
RewriteCond %{REQUEST_URI} !^/SubDomainName/
RewriteCond %{HTTP_HOST} !=SubDomainName.DomainName.com [NC]
#rewrite the url to force the use of https
RewriteRule ^(.*)$ https://DomainName.com/$1 [R=301,L]
So let explain the 2 critical new lines:
RewriteCond %{HTTP_HOST} !=SubDomainName.DomainName.com [NC]
This one is pretty self-explanatory, if a request is made to access SubDomainName.MyDomainName.com then you do not (!) apply the following Rewrite Rule.
RewriteCond %{REQUEST_URI} !^/SubDomainName/
This one on the other hand may not be so obvious. My host was automatically redirecting request for
SubDomainName.DomainName.com
to
DomainName.com/SubDomainName
so I needed to add another Rewrite Condition to also exclude such a request from being forced into https. You may not need to include this condition on how your host has things setup.
Robot.txt
One last additional tidbit. While trying to figure out the above I came across a posting in which they stated that an exclusion for the robot.txt should be made so it does not require https. To do so, you would add one more Rewrite Conditon
RewriteCond %{REQUEST_URI} !/robots.txt
So the final htaccess would then become
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
#Exclude robot.txt from rewrite rule
RewriteCond %{REQUEST_URI} !/robots.txt
#Exclude subdomain from rewrite rule
RewriteCond %{REQUEST_URI} !^/SubDomainName/
RewriteCond %{HTTP_HOST} !=SubDomainName.DomainName.com [NC]
#rewrite the url to force the use of https
RewriteRule ^(.*)$ https://DomainName.com/$1 [R=301,L]
</IfModule>
Resources
While trying to figure this all out, I came across a few good sites, should you wish to delve deeper into the matter:
Disclaimer
Let me be explicitly clear, I am not an htaccess experts by any means. I simply managed to piece the above together out of personal necessity. Just because it worked for me does not guarantee it will work for you, nor should any assumptions be made that it is the best approach to the matter.
As with any post on my site, I am always open to better ways of doing things, and learning, so feel free to leave a comment if you can add constructive information to the above.