24

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,

2 Answers 2

21

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.

But for the special case of mod_rewrite read "Rules inherited from the parent scope are applied after rules specified in the child scope."

3
  • 8
    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
    Commented Jan 19, 2015 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 Commented Jul 12, 2019 at 4:01
  • @GetFree Is this the documentation for the "sub rules then parent rules" behavior you stated?
    – zylstra
    Commented Apr 23, 2022 at 6:29
4

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.

3
  • 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
    Commented Jul 14, 2014 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
    Commented Oct 31, 2015 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
    Commented Feb 2, 2021 at 20:30

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.