~ / archivo / stackoverflow / 22703014
I need to force ssl on main domain but not subdomain
StackOverflow
· .htaccess, mod-rewrite, ssl
· ayudé
-
The main site at www.mydomain.com is set up as a secure site using an SSL certificate.
I need to create a subdomain that is not secured (http://open.mydomain.com).
I'm hosting with InMotionHosting.com. I've created the subdomain in cPanel and pointed it to the folder at public_html/open (the main site is at public_html/mainsite) but when I try to visit the subdomain URL I get an SSL error in Chrome, and in IE I get redirected to https://www.mydomain.com/open.
How do I fix this?
Here is my .htaccess file:
RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} -
Change your rules to this:
RewriteEngine on RewriteBase / RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]- user3470981 2014-03-28 14:37 Thank you for the suggestion. However, this no longer forces the main domain to SSL so it's possible to use the main site non-securely. Perhaps I wasn't clear on that, but I need www.mydomain.com to be secure ALWAYS and open.mydomain.com to be secure NEVER>
- anubhava 2014-03-28 15:12 Are you behind some proxy by any chance, otherwise 1st rule enforces
www.mydomain.comto be always https. Do you have other rules also? - user3470981 2014-03-28 17:05 No, not behind a proxy and the code you gave me is all that is in the .htaccess file.
- user3470981 2014-03-28 17:08 Actually, I'm going to modify this slightly. When I enter
www.mydomain.comormydomain.comin the browser, it is forced to https. But when I enterhttp://www.mydomain.comit is not -- it stays in unsecured mode and I can navigate through the site unsecured. - anubhava 2014-03-28 17:12 It is confusing. You wrote
www.mydomain.com or mydomain.com in the browser, it is forced to httpsthen wrote opposite thathttp://www.mydomain.comis not. Aren'twww.mydomain.comandhttp://www.mydomain.comsame?
-
This worked for me:
<IfModule mod_rewrite.c> RewriteCond %{SERVER_PORT} 80 RewriteCond %{HTTP_HOST} !^sub.example.com$ RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] </IfModule>If you want several subdomains change the line like this:
RewriteCond %{HTTP_HOST} !^(sub|lolo).example.com$