https for main domain and http for subdomain

2019-08-10 07:09发布

问题:

So i need very simple codes for .htaccess

I want to redirect all traffic to https in main domain and redirect all traffic to http in subdomain without WWW.

Right now i am using this code to redirect all traffic to https

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

So main domain should be like this: https://website.com

and subdomain should be like this: http://sub.website.com

I found many answers but very complicated, so need simple solution.

EDITED

Solution given by anubhava works great. Thanks for your solution.

回答1:

You can use:

RewriteEngine On

# maindomain -> https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]

# subdomain -> http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.website\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R,L]