spiderling spiderling - 2 months ago 13
Apache Configuration Question

redirect root domain only to https, not subdomain

I know this has been answered in other posts (http://stackoverflow.com/a/25196859/730378 and http://stackoverflow.com/a/20915595/730378), but I've tried them both and neither are working for me.

Using the htaccess code below works to redirect to https for the root domain and the aliases, but the subdomain keeps redirecting and using https for the subdomain as well, which I don't want.

####
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !=subdomain.rootdomain.ca [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?rootdomain.ca$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?rootdomain.biz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?rootdomain.co$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?domainalias.biz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?domainalias.ca$ [NC]
RewriteRule (.*) https://rootdomain.ca/$1 [R=301,L]

Answer

Your should use !^ instead of != on the first condition but technically you don't even need the first one. It will only redirect to root if it matches the root domains or aliases. Consolidate all those conditions and try your code this way. Clear your cache and give this try.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(rootdomain|domainalias)\.(ca|biz|co)$ [NC]
RewriteRule (.*) https://rootdomain.ca/$1 [R=301,L]