I am struggling with setting proper https redirect via a .htaccess file for my domains. I have the following domains, subdomains and directories:

public_html/     -> www.red.com
public_html/drk  -> dark.red.com
public_html/blue -> www.blue.com

How am I supposed to write the .htaccess file(s) so that the redirect works properly?

Currently I have this in my .htaccess file:

RewriteEngine On
RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^(www\.)?red\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://red.com%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?dark\.red\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://dark.red.com%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?blue\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://blue.com%{REQUEST_URI} [R=301,L,NE]

Redirect works for the main site (www.red.com) but not for the other two sites. If I remove the rules for the other two sites, redirect sends me to www.red.com/drk instead of dark.red.com and to www.red.com/blue instead of www.blue.com.

  • What are you trying to do here? If you are just trying to redirect HTTP to HTTPS, why do you think you can't do it with a single ruleset matching HTTP and rewriting for HTTPS? – davidgo Nov 5 '17 at 22:23
  • I tried with a single rule but I got inaccurate redirects. How would a correct single rule look like? – RegedUser00x Nov 6 '17 at 20:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.