DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.1 Placing Directives Properly

Problem

You know what directive you need but aren't sure where to put it.

Solution

If you wish the scope of the directive to be global (i.e., you want it to affect all requests to the web server), then it should be put in the main body of the configuration file or it should be put in the section starting with the line <Directory /> and ending with </Directory>.

If you wish the directive to affect only a particular directory, it should be put in a <Directory> section that specifies that directory. Be aware that directives specified in this manner also affect subdirectories of the stated directory.

Likewise, if you wish the directive to affect a particular virtual host or a particular set of URLs, then the directive should be put in a <VirtualHost> section, <Location> section, or perhaps a <Files> section, referring to the particular scope in which you want the directive to apply.

In short, the answer to "Where should I put it?" is "Where do you want it to be in effect?"

Discussion

This question is perhaps the most frequently asked question in every Apache help venue. It is usually answered in a way that is relevant to the specific situation but not in a general all-purpose kind of way.

The situation is further complicated by the fact that the configuration file is frequently split over several files, which are loaded via Include directives, and the (usually) mistaken impression that it will make a difference whether a directive is put in one file or another.

Knowing exactly where to put a particular directive comes from understanding how Apache deals with sections (such as <Directory> and <Location>). There is seldom one magic place that a directive must be placed to make it work. However, there are usually a number of places where you can put a directive and have it produce an undesired effect.

There are two main situations in which a directive, when added to your configuration file, will not have the desired effect. These are when a directive is overridden by a directive appearing in the same scope but later in the configuration, and when there is a directive in a more specific scope.

For the first of these two situations, it is important to understand that the Apache configuration file is parsed from top to bottom. Files that are Include'ed are considered to appear in their entirety in the location where the Include directive appears. Thus, if you have the same directive appearing twice but with different values, the last one appearing will be the one that is actually in effect.

In the other situation, it's important to understand that, while directives in one directory apply to subdirectories, a <Directory> section referring to a more specific or "deeper" directory will have precedence over sections referring to "shallower" directories. For example, consider the following configuration:

<Directory /www/docs>
    Options ExecCGI
</Directory>

<Directory /www/docs/mod>
    Options Includes
</Directory>

Files accessed from the directory /www/docs/mod/misc/ will have Options Includes in effect but will not have Options ExecCGI in effect, because the more specific directory section is the configuration that applies.

Finally, you must consider .htaccess files as well, which can override settings in the main server configuration file, and cause situations that are confusing and difficult to be tracked down.

See Also

For .htaccess files:

http://httpd.apache.org/docs/howto/htaccess.html
http://httpd.apache.org/docs-2.0/howto/htaccess.html

For directories:

http://httpd.apache.org/docs/mod/core.html#directory
http://httpd.apache.org/docs/mod/core.html#directorymatch
http://httpd.apache.org/docs-2.0/mod/core.html#directory
http://httpd.apache.org/docs-2.0/mod/core.html#directorymatch

For location:

http://httpd.apache.org/docs/mod/core.html#location
http://httpd.apache.org/docs/mod/core.html#locationmatch
http://httpd.apache.org/docs-2.0/mod/core.html#location
http://httpd.apache.org/docs-2.0/mod/core.html#locationmatch

For Apache:

http://httpd.apache.org/docs/mod/core.html#files
http://httpd.apache.org/docs/mod/core.html#filesmatch
http://httpd.apache.org/docs-2.0/mod/core.html#files
http://httpd.apache.org/docs-2.0/mod/core.html#filesmatch
    [ Team LiB ] Previous Section Next Section