Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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/modrewrite internals ... why is more important to me than how to fix this :) Thanks!

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.

2 Answers 2

up vote 8 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 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! –  LonelyPixel 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 Jul 14 '14 at 3:08

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.