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 professional system and network administrators.

2 Answers 2

up vote 7 down vote accepted

I suppose you should read the Apache Tutorial: .htaccess, mainly the section "How directives are applied", that 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 mistery anymore.

share|improve this answer

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
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 Jul 14 at 3:08

Your Answer

 
discard

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.