Redirect Primary Domain to https but all Sub-domains to http with mod_rewrite and htaccess

.htaccesshttphttpsmod-rewritewildcard-subdomain

The Details

I have a business host gator account..not bragging.. that comes with a free Private SSL and IP. I am running a wordpress MU install that uses the sub-domain setting and have domain mapping setup via a plugin they offer.

Assuming you know nothing about Worpress MU… basically the WordPress MU allows you to create multiple self hosted blogs under a single WordPress install. I have a wildcard sub-domain setup to catch all sub.domain.com requests and WordPress MU does some voodoo to redirect it to the proper destination.

The private SSL is FREE so I am not going to shell out for a wildcard SSL right now but I do know that is an option. With that said when I, when I click any link from the https on the primary domain to a sub-domain the https sticks to the URL.

Can anyone help me do the following?

(please note: I had to add space between http(s):// and www below to break links in order to post my question)

For the primary "blog" url I would like both example www.domain.com and domain.com to redirect to a secure https with the result being https:// www.domain.com

For the "wildcard" sub-domain "sub-blog" url I would like example sub.domain.com and https:// sub.domain.com to redirect away form ssl to http with the result being http:// sub.domain.com

Code example would be appreciated and a detailed (dummy) tutorial so I can understand what is going on would be great. I am new to mod_rewrite my skill level is limited to basic 301 redirect.

Thank you

Best Answer

You can use that in your root .htaccess:

RewriteEngine on 

# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]

# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Related Topic