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,


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.

  • 7
    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
  • 1
    The doc listed above is for Apache 2.2. For the current version of apache, go to this link instead httpd.apache.org/docs/current/howto/htaccess.html Jul 12 '19 at 4:01

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.

  • 1
    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
  • 2
    @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.
    – MrWhite
    Oct 31 '15 at 15:21
  • 1
    Apache v2.3.10 allows you to use InheritBefore, and if you're using v2.4.8 or higher you can apply InheritDownBefore from the parent directory which forces all children below it to inherit the rules.
    – Charles
    Feb 2 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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