1

In the root level .htaccess I have added a condition to rewrite HTTP requests as HTTPS:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This applies to the main domain and all subdomains. Unfortunately there is still work to do in one of the subdomains in order for it to work with HTTPS, so I would like to insert something into that one's .htaccess file to undo the inherited rewrite rules.

The file must still inherit a lot of other config from the parent domain, so it must only undo that one rewrite rule.

How can I disable this rewrite rule for a particular subdomain?

1 Answer 1

2

If the subdomain points to a subdirectory off the main domain's document root then you can simply include a RewriteEngine On directive in that subdirectory's .htaccess file. However, this will override all the mod_rewrite directives in the parent .htaccess file, which may not be desirable (although "other config from the parent domain" will be inherited).

If you have other mod_rewrite directives in the parent .htaccess that you do want to run then you will need to add a condition to this HTTP to HTTPS rule in the parent .htaccess file that specifically excludes the subdomain.

For example:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain\.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

Which says... if the host does not start subdomain. and is not https then...

You normally require the L flag on redirects so processing does not continue through the file (if you have more mod_rewrite directives). Also, this shopuld normally be a 301 (permanent) redirect. If this is the case then change the 302 to 301 when you are sure it's working OK. Although maybe best left as a 302 whilst this is being developed.

Also, no need to capture the URL-path (ie. (.*)) if this is not being used in the RewriteRule substitution (or RewriteCond directives).

1
  • 1
    Excellent, well-thought-out answer! The bit about the RewriteRules overriding each other is particularly important.
    – Itai
    Commented Sep 21, 2017 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.