1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "./style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="./style/manual.en.xsl"?>
6 <relativepath href="."/>
8 <title>Configuration Sections</title>
10 <summary> <p>Directives in the <a
11 href="configuring.html">configuration files</a> may apply to the
12 entire server, or they may be restricted to apply only to particular
13 directories, files, hosts, or URLs. This document describes how to
14 use configuration section containers or <code>.htaccess</code> files
15 to change the scope of other configuration directives.</p>
18 <section id="types"><title>Types of Configuration Section Containers</title>
23 <module>mod_proxy</module>
26 <directive type="section" module="core">Directory</directive>
27 <directive type="section" module="core">DirectoryMatch</directive>
28 <directive type="section" module="core">Files</directive>
29 <directive type="section" module="core">FilesMatch</directive>
30 <directive type="section" module="core">IfDefine</directive>
31 <directive type="section" module="core">IfModule</directive>
32 <directive type="section" module="core">Location</directive>
33 <directive type="section" module="core">LocationMatch</directive>
34 <directive type="section" module="proxy">Proxy</directive>
35 <directive type="section" module="core">VirtualHost</directive>
39 <p>There are two basic types of containers. Most containers are
40 evaluated for each request. The enclosed directives are applied only
41 for those requests that match the containers. The <directive
42 type="section" module="core">IfDefine</directive> and <directive
43 type="section" module="core">IfModule</directive> containers, on the
44 other hand, are evaluated only at server startup and restart. If
45 their conditions are true at startup, then the enclosed directives
46 will apply to all requests. If the conditions are not true, the
47 enclosed directives will be ignored.</p>
49 <p>The <directive type="section" module="core">IfDefine</directive> directive
50 encloses directives that will only be applied if an appropriate
51 parameter is defined on the <code>httpd</code> command line. For example,
52 with the following configuration, all requests will be redirected
53 to another site only if the server is started using
54 <code>httpd -DClosedForNow</code>:</p>
57 <IfDefine ClosedForNow><br />
58 Redirect / http://otherserver.example.com/<br />
62 <p>The <directive type="section" module="core">IfModule</directive>
63 directive is very similar, except it encloses directives that will
64 only be applied if a particular module is available in the server.
65 The module must either be statically compiled in the server, or it
66 must be dynamically compiled and its <directive
67 module="mod_so">LoadModule</directive> line must be earlier in the
68 configuration file. This directive should only be used if you need
69 your configuration file to work whether or not certain modules are
70 installed. It should not be used to enclose directives that you want
71 to work all the time, because it can suppress useful error messages
72 about missing modules.</p>
74 <p>In the following example, the <directive
75 module="mod_mime_magic">MimeMagicFiles</directive> directive will be
76 applied only if <module>mod_mime_magic</module> is available.</p>
79 <IfModule mod_mime_magic.c><br />
80 MimeMagicFile conf/magic<br />
84 <p>Both <directive type="section" module="core">IfDefine</directive>
85 and <directive type="section" module="core">IfModule</directive>
86 can apply negative conditions by preceding their test with "!".
87 Also, these sections can be nested to achieve more complex
91 <section id="file-and-web"><title>Filesystem and Webspace</title>
93 <p>The most commonly used configuration section containers are the
94 ones that change the configuration of particular places in the
95 filesystem or webspace. First, it is important to understand the
96 difference between the two. The filesystem is the view of your disks
97 as seen by your operating system. For example, in a default install,
98 Apache resides at <code>/usr/local/apache2</code> in the Unix
99 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
100 the Windows filesystem. (Note that forward slashes should always be
101 used as the path separator in Apache, even for Windows.) In contrast,
102 the webspace is the view of your site as delivered by the web server
103 and seen by the client. So the path <code>/dir/</code> in the
104 webspace corresponds to the path
105 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
106 default Apache install on Unix. The webspace need not map directly to
107 the filesystem, since webpages may be generated dynamically
108 from databases or other locations.</p>
110 <section id="filesystem"><title>Filesystem Containers</title>
112 <p>The <directive type="section" module="core">Directory</directive>
113 and <directive type="section" module="core">Files</directive>
114 directives, along with their regex counterparts, apply directives to
115 parts of the filesystem. Directives enclosed in a <directive
116 type="section" module="core">Directory</directive> section apply to
117 the named filesystem directory and all subdirectories of that
118 directory. The same effect can be obtained using <a
119 href="howto/htaccess.html">.htaccess files</a>. For example, in the
120 following configuration, directory indexes will be enabled for the
121 <code>/var/web/dir1</code> directory and all subdirectories.</p>
124 <Directory /var/web/dir1><br />
125 Options +Indexes<br />
129 <p>Directives enclosed in a <directive type="section"
130 module="core">Files</directive> section apply to any file with
131 the specified name, regardless of what directory it lies in.
132 So for example, the following configuration directives will,
133 when placed in the main section of the configuration file,
134 deny access to any file named <code>private.html</code> regardless
135 of where it is found.</p>
138 <Files private.html><br />
139 Order allow,deny<br />
144 <p>To address files found in a particular part of the filesystem, the
145 <directive type="section" module="core">Files</directive> and
146 <directive type="section" module="core">Directory</directive> sections
147 can be combined. For example, the following configuration will deny
148 access to <code>/var/web/dir1/private.html</code>,
149 <code>/var/web/dir1/subdir2/private.html</code>,
150 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
151 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
155 <Directory /var/web/dir1><br />
156 <Files private.html><br />
157 Order allow,deny<br />
164 <section id="webspace"><title>Webspace Containers</title>
166 <p>The <directive type="section" module="core">Location</directive>
167 directive and its regex counterpart, on the other hand, change the
168 configuration for content in the webspace. For example, the following
169 configuration prevents access to any URL-path that begins in /private.
170 In particular, it will apply to requests for
171 <code>http://yoursite.example.com/private</code>,
172 <code>http://yoursite.example.com/private123</code>, and
173 <code>http://yoursite.example.com/private/dir/file.html</code> as well
174 as any other requests starting with the <code>/private</code> string.</p>
177 <Location /private><br />
178 Order Allow,Deny<br />
183 <p>The <directive type="section" module="core">Location</directive>
184 directive need not have anything to do with the filesystem.
185 For example, the following example shows how to map a particular
186 URL to an internal Apache handler provided by <module>mod_status</module>.
187 No file called <code>server-status</code> needs to exist in the
191 <Location /server-status><br />
192 SetHandler server-status<br />
197 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
199 <p>The <directive type="section" module="core">Directory</directive>,
200 <directive type="section" module="core">Files</directive>, and
201 <directive type="section" module="core">Location</directive>
202 directives can each use the shell-style wildcard characters "?" to
203 match any single character, "*" to match any set of characters, and
204 character classes like [a-zA-Z] to match particular characters. This
205 is useful to apply the same configuration to a group of filesystem or
206 webspace locations. If even more flexible matching is required, each
207 container has a regular-expression (regex) counterpart <directive
208 type="section" module="core">DirectoryMatch</directive>, <directive
209 type="section" module="core">FilesMatch</directive>, and <directive
210 type="section" module="core">LocationMatch</directive> that allow
211 perl-compatible<a href="glossary.html#regex">regular expressions</a>
212 to be used in choosing the matches. But see the section below on
213 configuration merging to find out how using regex sections will change
214 how directives are applied.</p>
216 <p>A non-regex wildcard section that changes the configuration of
217 all user directories could look as follows:</p>
220 <Directory /home/*/public_html><br />
221 Options Indexes<br />
225 <p>Using regex sections, we can deny access to many types of image files
228 <FilesMatch \.(?i:gif|jpe?g|png)$><br />
229 Order allow,deny<br />
236 <section id="whichwhen"><title>What to use When</title>
238 <p>Choosing between filesystem containers and webspace containers is
239 actually quite easy. When applying directives to objects that reside
240 in the filesystem always use <directive type="section"
241 module="core">Directory</directive> or <directive type="section"
242 module="core">Files</directive>. When applying directives to objects
243 that do not reside in the filesystem (such as a webpage generated from
244 a database), use <directive type="section"
245 module="core">Location</directive>.</p>
247 <p>It is important to never use <directive type="section"
248 module="core">Location</directive> when trying to restrict
249 access to objects in the filesystem. This is because many
250 different webspace locations (URLs) could map to the same filesystem
251 location, allowing your restrictions to be circumvented.
252 For example, consider the following configuration:</p>
255 <Location /dir/><br />
256 Order allow,deny<br />
261 <p>This works fine if the request is for
262 <code>http://yoursite.example.com/dir/</code>. But what if you are on
263 a case-insensitive filesystem? Then your restriction could be easily
264 circumvented by requesting
265 <code>http://yoursite.example.com/DIR/</code>. The <directive
266 type="section" module="core">Directory</directive> directive, in
267 contrast, will apply to any content served from that location,
268 regardless of how it is called. (An exception is filesystem links.
269 The same directory can be placed in more than one part of the
270 filesystem using symbolic links. The <directive type="section"
271 module="core">Directory</directive> directive will follow the symbolic
272 link without resetting the pathname. Therefore, for the highest level
273 of security, symbolic links should be disabled with the appropriate
274 <directive module="core">Options</directive> directive.)</p>
276 <p>If you are, perhaps, thinking that none of this applies to you
277 because you use a case-sensitive filesystem, remember that there are
278 many other ways to map multiple webspace locations to the same
279 filesystem location. Therefore you should always use the filesystem
280 containers when you can. There is, however, one exception to this
281 rule. Putting configuration restrictions in a <code><Location
282 /></code> section is perfectly safe because this section will apply
283 to all requests regardless of the specific URL.</p>
288 <section id="virtualhost"><title>Virtual Hosts</title>
290 <p>The <directive type="section" module="core">VirtualHost</directive>
291 container encloses directives that apply to specific hosts.
292 This is useful when serving multiple hosts from the same machine
293 with a different configuration for each. For more information,
294 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
297 <section id="proxy"><title>Proxy</title>
299 <p>The <directive type="section" module="proxy">Proxy</directive>
300 container applies enclosed configuration directives only
301 to sites accessed through <module>mod_proxy</module>'s proxy server
302 that match the specified URL. For example, the following configuration
303 will prevent the proxy server from being used to access the
304 <code>cnn.com</code> website.</p>
307 <Proxy http://cnn.com/*><br />
308 Order allow,deny<br />
314 <section id="whatwhere"><title>What Directives are Allowed?</title>
316 <p>To find out what directives are allowed in what types of
317 configuration sections, check the <a
318 href="mod/directive-dict.html#Context">Context</a> of the directive.
319 Everything that is allowed in
320 <directive type="section" module="core">Directory</directive>
321 sections is also syntactically allowed in
322 <directive type="section" module="core">DirectoryMatch</directive>,
323 <directive type="section" module="core">Files</directive>,
324 <directive type="section" module="core">FilesMatch</directive>,
325 <directive type="section" module="core">Location</directive>,
326 <directive type="section" module="core">LocationMatch</directive>,
328 <directive type="section" module="proxy">Proxy</directive>
329 sections. There are some exceptions, however.</p>
332 <li>The <directive module="core">AllowOverride</directive> directive
333 works only in <directive type="section" module="core">Directory</directive>
336 <li>The <code>FollowSymLinks</code> and
337 <code>SymLinksIfOwnerMatch</code> <directive
338 module="core">Options</directive> work only in <directive
339 type="section" module="core">Directory</directive> sections or
340 <code>.htaccess</code> files.</li>
342 <li>The <directive module="core">Options</directive> directive cannot
343 be used in <directive type="section" module="core">Files</directive>
344 and <directive type="section" module="core">FilesMatch</directive>
349 <section id="mergin"><title>How the sections are merged</title>
351 <p>The configuration sections are applied in a very particular order.
352 Since this can have important effects on how configuration directives
353 are interpreted, it is important to understand how this works.</p>
355 <p>The order of merging is:</p>
358 <li> <directive type="section"
359 module="core">Directory</directive> (except regular expressions)
360 and .htaccess done simultaneously (with .htaccess, if allowed,
361 overriding <directive type="section"
362 module="core">Directory</directive>)</li>
364 <li><directive type="section" module="core">DirectoryMatch</directive>
365 (and <code><Directory ~></code>)</li>
367 <li><directive type="section"
368 module="core">Files</directive> and <directive
369 type="section" module="core">FilesMatch</directive> done
372 <li><directive type="section" module="core">Location</directive>
373 and <directive type="section"
374 module="core">LocationMatch</directive> done simultaneously</li>
377 <p>Apart from <directive type="section"
378 module="core">Directory</directive>, each group is processed in
379 the order that they appear in the configuration files. <directive
380 type="section" module="core">Directory</directive> (group 1 above)
381 is processed in the order shortest directory component to longest.
382 So for example, <code><Directory /var/web/dir></code> will
383 be processed before <code><Directory
384 /var/web/dir/subdir></code>. If multiple <directive
385 type="section" module="core">Directory</directive> sections apply
386 to the same directory they are processed in the configuration file
387 order. Configurations included via the <directive
388 module="core">Include</directive> directive will be treated as if
389 they were inside the including file at the location of the
390 <directive module="core">Include</directive> directive.</p>
392 <p>Sections inside <directive type="section"
393 module="core">VirtualHost</directive> sections
394 are applied <em>after</em> the corresponding sections outside
395 the virtual host definition. This allows virtual hosts to
396 override the main server configuration.</p>
398 <p>Later sections override earlier ones.</p>
400 <note><title>Technical Note</title>
402 <code><Location></code>/<code><LocationMatch></code>
403 sequence performed just before the name translation phase
404 (where <code>Aliases</code> and <code>DocumentRoots</code>
405 are used to map URLs to filenames). The results of this
406 sequence are completely thrown away after the translation has
410 <section id="merge-examples"><title>Some Examples</title>
412 <p>Below is an artificial example to show the order of
413 merging. Assuming they all apply to the request, the directives in
414 this example will be applied in the order A > B > C > D >
418 <Location /><br />
420 </Location><br />
422 <Files f.html><br />
426 <VirtualHost *><br />
427 <Directory /a/b><br />
429 </Directory><br />
430 </VirtualHost><br />
432 <DirectoryMatch "^.*b$"><br />
434 </DirectoryMatch><br />
436 <Directory /a/b><br />
438 </Directory><br />
442 <p>For a more concrete example, consider the following. Regardless of
443 any access restrictions placed in <directive module="core"
444 type="section">Directory</directive> sections, the <directive
445 module="core" type="section">Location</directive> section will be
446 evaluated last and will allow unrestricted access to the server. In
447 other words, order of merging is important, so be careful!</p>
450 <Location /><br />
451 Order deny,allow<br />
453 </Location><br />
455 # Woops! This <Directory> section will have no effect<br />
456 <Directory /><br />
457 Order allow,deny<br />
459 Deny from badguy.example.com<br />