How multiple <Directory> sections work in Apache configuration (httpd.conf)?

6,224

1. Isn't the RewriteEngine On in the first <Directory> section supposed to apply to the second <Directory> section as well, considering that the latter is a sub-directory? But I noticed that I need to add the RewriteEngine On rule in the second <Directory> section as well.

This answer by Jon Lin on what the RewriteOptions Inherit directive does is an eye opener, and indirectly answers this question.

"Normally, if you have an .htaccess file in a particular sub-directory, any [mod_rewrite configuration settings] in a parent directory are superseded and won't be applied."

Related Links:

2. Does this mean that I should also copy all the common rules from the first <Directory> section to the second <Directory> section? For example, AllowOverride None, Options -MultiViews, etc.

No, since all rules for '/var/www/example.com/public' (set in <Directory /var/www/example.com/public> section) also apply to its sub-directories, there's no need to copy them again into <Directory /var/www/example.com/public/wp-content/cache/minify> section.

Finding this out was easy.

I added this inside the <Directory /var/www/example.com/public> section of my httpd.conf:

<IfModule mod_headers.c>    
    # Set noindex header for robots.txt
    <FilesMatch "robots.txt">
        Header set X-Robots-Tag "noindex"
    </FilesMatch>
</IfModule>

Then dropped a robots.txt file in '/var/www/example.com/public' and '/var/www/example.com/public/wp-content/cache/minify' directories. Both were served with noindex HTTP header.

Share:
6,224

Related videos on Youtube

its_me
Author by

its_me

Updated on September 18, 2022

Comments

  • its_me
    its_me over 1 year

    For example, if my '/etc/apache2/httpd.conf' looks like this:

    <Directory />
        AllowOverride None
    </Directory>
    
    <Directory /home>
        AllowOverride FileInfo
    </Directory>
    

    The latter AllowOverride rule overrides its predecessor for the /home directory. That part is clear.

    But what happens if my httpd.conf file looks like this?

    <Directory /var/www/example.com/public>
    
        AllowOverride None
        Options -MultiViews
    
        <IfModule mod_rewrite.c>
    
            RewriteEngine On
            RewriteBase /
    
            [...]
    
        </IfModule>
    
        [...]
    
    </Directory>
    
    <Directory /var/www/example.com/public/wp-content/cache/minify>
        <IfModule mod_rewrite.c>
    
            # WHY IS 'RewriteEngine On' REQUIRED?
            RewriteEngine On
    
            RewriteBase /wp-content/cache/minify/
            RewriteRule [...]
    
        </IfModule>
    </Directory>
    

    QUESTIONS:

    1. Isn't the RewriteEngine On in the first <Directory> section supposed to apply to the second <Directory> section as well, considering that the latter is a sub-directory? But I noticed that I need to add the RewriteEngine On rule in the second <Directory> section as well.

    2. Does this mean that I should also copy all the common rules from the first <Directory> section to the second <Directory> section? For example, AllowOverride None, Options -MultiViews, etc.