Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone explain to me in what order apache executes .htaccess files residing in different levels of the same path and how the rewrite rules therein are prioritized?

For example, why doesn't the rewrite rule in the first .htaccess below work and is the one in /blog prioritized?

.htaccess in /

RewriteEngine on
RewriteBase /
RewriteRule ^blog offline.html [L]

.htaccess in /blog

RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

PS: I'm not simply looking for an answer but for a way to understand the apache/mod_rewrite internals ... why is more important to me than how to fix this,

share|improve this question

migrated from serverfault.com Feb 21 '11 at 21:43

This question came from our site for system and network administrators.

up vote 13 down vote accepted

I suppose you should read the Apache Tutorial: .htaccess, mainly the section "How directives are applied", which states:

The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found. Therefore, a .htaccess file in a particular directory may override directives found in .htaccess files found higher up in the directory tree. And those, in turn, may have overridden directives found yet higher up, or in the main server configuration file itself.

Give it a full read and .htaccess will be no mystery anymore.

share|improve this answer
mod_rewrite is a special case. Rewrite rules are applied bottom up. First the rules in the subdirectory then the rules in the parent directory. – GetFree Jan 19 '15 at 23:12

You can use RewriteOptions inherit to inherit the rewrite rules of the parent directory. However, the parent directory's rewrite rules will be applied later, as per the documentation on RewriteOptions.

share|improve this answer
This was exactly what I was missing, thank you! – ygoe Mar 11 '12 at 11:19
Rewrite directives for parent directories will be applied even without this statement (which is somewhat confusing) but I suppose this can be used to perform a virtual copy + paste of directives from the parent to the child (which may have different rewrite bases). No wonder mod_rewrite messes with your head sometimes... Argh. – Simon East Jul 14 '14 at 3:08
@SimonEast "Rewrite directives for parent directories will be applied even without this statement" - Are you sure? Do you have a reference? Specifically, mod_rewrite directives are not inherited by default (different to other modules). You would need a RewriteOptions InheritXXXX directive somewhere for directives to be inherited. However, this gets more complex on Apache 2.4+ with the InheritDown option that can be specified in parent directories and the server config which then controls child directories. – w3dk Oct 31 '15 at 15:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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