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

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:44:41

问题


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


回答1:


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]


来源:https://stackoverflow.com/questions/27882723/redirect-primary-domain-to-https-but-all-sub-domains-to-http-with-mod-rewrite-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!