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"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="sections.xml.meta">
25 <title>Configuration Sections</title>
27 <summary> <p>Directives in the <a
28 href="configuring.html">configuration files</a> may apply to the
29 entire server, or they may be restricted to apply only to particular
30 directories, files, hosts, or URLs. This document describes how to
31 use configuration section containers or <code>.htaccess</code> files
32 to change the scope of other configuration directives.</p>
35 <section id="types"><title>Types of Configuration Section Containers</title>
40 <module>mod_version</module>
41 <module>mod_proxy</module>
44 <directive type="section" module="core">Directory</directive>
45 <directive type="section" module="core">DirectoryMatch</directive>
46 <directive type="section" module="core">Files</directive>
47 <directive type="section" module="core">FilesMatch</directive>
48 <directive type="section" module="core">IfDefine</directive>
49 <directive type="section" module="core">IfModule</directive>
50 <directive type="section" module="mod_version">IfVersion</directive>
51 <directive type="section" module="core">Location</directive>
52 <directive type="section" module="core">LocationMatch</directive>
53 <directive type="section" module="mod_proxy">Proxy</directive>
54 <directive type="section" module="mod_proxy">ProxyMatch</directive>
55 <directive type="section" module="core">VirtualHost</directive>
59 <p>There are two basic types of containers. Most containers are
60 evaluated for each request. The enclosed directives are applied only
61 for those requests that match the containers. The <directive
62 type="section" module="core">IfDefine</directive>, <directive
63 type="section" module="core">IfModule</directive>, and
64 <directive type="section" module="mod_version">IfVersion</directive>
65 containers, on the other hand, are evaluated only at server startup
66 and restart. If their conditions are true at startup, then the
67 enclosed directives will apply to all requests. If the conditions are
68 not true, the enclosed directives will be ignored.</p>
70 <p>The <directive type="section" module="core">IfDefine</directive> directive
71 encloses directives that will only be applied if an appropriate
72 parameter is defined on the <program>httpd</program> command line. For example,
73 with the following configuration, all requests will be redirected
74 to another site only if the server is started using
75 <code>httpd -DClosedForNow</code>:</p>
78 <IfDefine ClosedForNow><br />
79 Redirect / http://otherserver.example.com/<br />
83 <p>The <directive type="section" module="core">IfModule</directive>
84 directive is very similar, except it encloses directives that will
85 only be applied if a particular module is available in the server.
86 The module must either be statically compiled in the server, or it
87 must be dynamically compiled and its <directive
88 module="mod_so">LoadModule</directive> line must be earlier in the
89 configuration file. This directive should only be used if you need
90 your configuration file to work whether or not certain modules are
91 installed. It should not be used to enclose directives that you want
92 to work all the time, because it can suppress useful error messages
93 about missing modules.</p>
95 <p>In the following example, the <directive
96 module="mod_mime_magic">MimeMagicFiles</directive> directive will be
97 applied only if <module>mod_mime_magic</module> is available.</p>
100 <IfModule mod_mime_magic.c><br />
101 MimeMagicFile conf/magic<br />
105 <p>The <directive type="section" module="mod_version">IfVersion</directive>
106 directive is very similar to <directive type="section"
107 module="core">IfDefine</directive> and <directive type="section"
108 module="core">IfModule</directive>, except it encloses directives that will
109 only be applied if a particular version of the server is executing. This
110 module is designed for the use in test suites and large networks which have to
111 deal with different httpd versions and different configurations.</p>
114 <IfVersion >= 2.1><br />
116 # this happens only in versions greater or<br />
122 <p><directive type="section" module="core">IfDefine</directive>,
123 <directive type="section" module="core">IfModule</directive>, and the
124 <directive type="section" module="mod_version">IfVersion</directive>
125 can apply negative conditions by preceding their test with "!".
126 Also, these sections can be nested to achieve more complex
130 <section id="file-and-web"><title>Filesystem and Webspace</title>
132 <p>The most commonly used configuration section containers are the
133 ones that change the configuration of particular places in the
134 filesystem or webspace. First, it is important to understand the
135 difference between the two. The filesystem is the view of your disks
136 as seen by your operating system. For example, in a default install,
137 Apache resides at <code>/usr/local/apache2</code> in the Unix
138 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
139 the Windows filesystem. (Note that forward slashes should always be
140 used as the path separator in Apache, even for Windows.) In contrast,
141 the webspace is the view of your site as delivered by the web server
142 and seen by the client. So the path <code>/dir/</code> in the
143 webspace corresponds to the path
144 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
145 default Apache install on Unix. The webspace need not map directly to
146 the filesystem, since webpages may be generated dynamically
147 from databases or other locations.</p>
149 <section id="filesystem"><title>Filesystem Containers</title>
151 <p>The <directive type="section" module="core">Directory</directive>
152 and <directive type="section" module="core">Files</directive>
153 directives, along with their <glossary ref="regex">regex</glossary>
154 counterparts, apply directives to
155 parts of the filesystem. Directives enclosed in a <directive
156 type="section" module="core">Directory</directive> section apply to
157 the named filesystem directory and all subdirectories of that
158 directory. The same effect can be obtained using <a
159 href="howto/htaccess.html">.htaccess files</a>. For example, in the
160 following configuration, directory indexes will be enabled for the
161 <code>/var/web/dir1</code> directory and all subdirectories.</p>
164 <Directory /var/web/dir1><br />
165 Options +Indexes<br />
169 <p>Directives enclosed in a <directive type="section"
170 module="core">Files</directive> section apply to any file with
171 the specified name, regardless of what directory it lies in.
172 So for example, the following configuration directives will,
173 when placed in the main section of the configuration file,
174 deny access to any file named <code>private.html</code> regardless
175 of where it is found.</p>
178 <Files private.html><br />
179 Order allow,deny<br />
184 <p>To address files found in a particular part of the filesystem, the
185 <directive type="section" module="core">Files</directive> and
186 <directive type="section" module="core">Directory</directive> sections
187 can be combined. For example, the following configuration will deny
188 access to <code>/var/web/dir1/private.html</code>,
189 <code>/var/web/dir1/subdir2/private.html</code>,
190 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
191 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
195 <Directory /var/web/dir1><br />
196 <Files private.html><br />
197 Order allow,deny<br />
204 <section id="webspace"><title>Webspace Containers</title>
206 <p>The <directive type="section" module="core">Location</directive>
207 directive and its <glossary ref="regex">regex</glossary> counterpart, on
208 the other hand, change the
209 configuration for content in the webspace. For example, the following
210 configuration prevents access to any URL-path that begins in /private.
211 In particular, it will apply to requests for
212 <code>http://yoursite.example.com/private</code>,
213 <code>http://yoursite.example.com/private123</code>, and
214 <code>http://yoursite.example.com/private/dir/file.html</code> as well
215 as any other requests starting with the <code>/private</code> string.</p>
218 <Location /private><br />
219 Order Allow,Deny<br />
224 <p>The <directive type="section" module="core">Location</directive>
225 directive need not have anything to do with the filesystem.
226 For example, the following example shows how to map a particular
227 URL to an internal Apache handler provided by <module>mod_status</module>.
228 No file called <code>server-status</code> needs to exist in the
232 <Location /server-status><br />
233 SetHandler server-status<br />
238 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
240 <p>The <directive type="section" module="core">Directory</directive>,
241 <directive type="section" module="core">Files</directive>, and
242 <directive type="section" module="core">Location</directive>
243 directives can each use shell-style wildcard characters as in
244 <code>fnmatch</code> from the C standard library. The character "*"
245 matches any sequence of characters, "?" matches any single character,
246 and "[<em>seq</em>]" matches any character in <em>seq</em>. The "/"
247 character will not be matched by any wildcard; it must be specified
250 <p>If even more flexible matching is required, each
251 container has a regular expression (regex) counterpart <directive
252 type="section" module="core">DirectoryMatch</directive>, <directive
253 type="section" module="core">FilesMatch</directive>, and <directive
254 type="section" module="core">LocationMatch</directive> that allow
256 <glossary ref="regex">regular expressions</glossary>
257 to be used in choosing the matches. But see the section below on
258 configuration merging to find out how using regex sections will change
259 how directives are applied.</p>
261 <p>A non-regex wildcard section that changes the configuration of
262 all user directories could look as follows:</p>
265 <Directory /home/*/public_html><br />
266 Options Indexes<br />
270 <p>Using regex sections, we can deny access to many types of image files
273 <FilesMatch \.(?i:gif|jpe?g|png)$><br />
274 Order allow,deny<br />
281 <section id="whichwhen"><title>What to use When</title>
283 <p>Choosing between filesystem containers and webspace containers is
284 actually quite easy. When applying directives to objects that reside
285 in the filesystem always use <directive type="section"
286 module="core">Directory</directive> or <directive type="section"
287 module="core">Files</directive>. When applying directives to objects
288 that do not reside in the filesystem (such as a webpage generated from
289 a database), use <directive type="section"
290 module="core">Location</directive>.</p>
292 <p>It is important to never use <directive type="section"
293 module="core">Location</directive> when trying to restrict
294 access to objects in the filesystem. This is because many
295 different webspace locations (URLs) could map to the same filesystem
296 location, allowing your restrictions to be circumvented.
297 For example, consider the following configuration:</p>
300 <Location /dir/><br />
301 Order allow,deny<br />
306 <p>This works fine if the request is for
307 <code>http://yoursite.example.com/dir/</code>. But what if you are on
308 a case-insensitive filesystem? Then your restriction could be easily
309 circumvented by requesting
310 <code>http://yoursite.example.com/DIR/</code>. The <directive
311 type="section" module="core">Directory</directive> directive, in
312 contrast, will apply to any content served from that location,
313 regardless of how it is called. (An exception is filesystem links.
314 The same directory can be placed in more than one part of the
315 filesystem using symbolic links. The <directive type="section"
316 module="core">Directory</directive> directive will follow the symbolic
317 link without resetting the pathname. Therefore, for the highest level
318 of security, symbolic links should be disabled with the appropriate
319 <directive module="core">Options</directive> directive.)</p>
321 <p>If you are, perhaps, thinking that none of this applies to you
322 because you use a case-sensitive filesystem, remember that there are
323 many other ways to map multiple webspace locations to the same
324 filesystem location. Therefore you should always use the filesystem
325 containers when you can. There is, however, one exception to this
326 rule. Putting configuration restrictions in a <code><Location
327 /></code> section is perfectly safe because this section will apply
328 to all requests regardless of the specific URL.</p>
333 <section id="virtualhost"><title>Virtual Hosts</title>
335 <p>The <directive type="section" module="core">VirtualHost</directive>
336 container encloses directives that apply to specific hosts.
337 This is useful when serving multiple hosts from the same machine
338 with a different configuration for each. For more information,
339 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
342 <section id="proxy"><title>Proxy</title>
344 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
345 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
346 containers apply enclosed configuration directives only
347 to sites accessed through <module>mod_proxy</module>'s proxy server
348 that match the specified URL. For example, the following configuration
349 will prevent the proxy server from being used to access the
350 <code>cnn.com</code> website.</p>
353 <Proxy http://cnn.com/*><br />
354 Order allow,deny<br />
360 <section id="whatwhere"><title>What Directives are Allowed?</title>
362 <p>To find out what directives are allowed in what types of
363 configuration sections, check the <a
364 href="mod/directive-dict.html#Context">Context</a> of the directive.
365 Everything that is allowed in
366 <directive type="section" module="core">Directory</directive>
367 sections is also syntactically allowed in
368 <directive type="section" module="core">DirectoryMatch</directive>,
369 <directive type="section" module="core">Files</directive>,
370 <directive type="section" module="core">FilesMatch</directive>,
371 <directive type="section" module="core">Location</directive>,
372 <directive type="section" module="core">LocationMatch</directive>,
373 <directive type="section" module="mod_proxy">Proxy</directive>,
374 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
375 sections. There are some exceptions, however:</p>
378 <li>The <directive module="core">AllowOverride</directive> directive
379 works only in <directive type="section" module="core">Directory</directive>
382 <li>The <code>FollowSymLinks</code> and
383 <code>SymLinksIfOwnerMatch</code> <directive
384 module="core">Options</directive> work only in <directive
385 type="section" module="core">Directory</directive> sections or
386 <code>.htaccess</code> files.</li>
388 <li>The <directive module="core">Options</directive> directive cannot
389 be used in <directive type="section" module="core">Files</directive>
390 and <directive type="section" module="core">FilesMatch</directive>
395 <section id="mergin"><title>How the sections are merged</title>
397 <p>The configuration sections are applied in a very particular order.
398 Since this can have important effects on how configuration directives
399 are interpreted, it is important to understand how this works.</p>
401 <p>The order of merging is:</p>
404 <li> <directive type="section"
405 module="core">Directory</directive> (except regular expressions)
406 and <code>.htaccess</code> done simultaneously (with
407 <code>.htaccess</code>, if allowed, overriding
408 <directive type="section" module="core">Directory</directive>)</li>
410 <li><directive type="section" module="core">DirectoryMatch</directive>
411 (and <code><Directory ~></code>)</li>
413 <li><directive type="section"
414 module="core">Files</directive> and <directive
415 type="section" module="core">FilesMatch</directive> done
418 <li><directive type="section" module="core">Location</directive>
419 and <directive type="section"
420 module="core">LocationMatch</directive> done simultaneously</li>
423 <p>Apart from <directive type="section"
424 module="core">Directory</directive>, each group is processed in
425 the order that they appear in the configuration files. <directive
426 type="section" module="core">Directory</directive> (group 1 above)
427 is processed in the order shortest directory component to longest.
428 So for example, <code><Directory /var/web/dir></code> will
429 be processed before <code><Directory
430 /var/web/dir/subdir></code>. If multiple <directive
431 type="section" module="core">Directory</directive> sections apply
432 to the same directory they are processed in the configuration file
433 order. Configurations included via the <directive
434 module="core">Include</directive> directive will be treated as if
435 they were inside the including file at the location of the
436 <directive module="core">Include</directive> directive.</p>
438 <p>Sections inside <directive type="section"
439 module="core">VirtualHost</directive> sections
440 are applied <em>after</em> the corresponding sections outside
441 the virtual host definition. This allows virtual hosts to
442 override the main server configuration.</p>
444 <p>When the request is served by <module>mod_proxy</module>, the
445 <directive module="mod_proxy" type="section">Proxy</directive>
446 container takes the place of the <directive module="core"
447 type="section">Directory</directive> container in the processing
450 <p>Later sections override earlier ones.</p>
452 <note><title>Technical Note</title>
454 <code><Location></code>/<code><LocationMatch></code>
455 sequence performed just before the name translation phase
456 (where <code>Aliases</code> and <code>DocumentRoots</code>
457 are used to map URLs to filenames). The results of this
458 sequence are completely thrown away after the translation has
462 <section id="merge-examples"><title>Some Examples</title>
464 <p>Below is an artificial example to show the order of
465 merging. Assuming they all apply to the request, the directives in
466 this example will be applied in the order A > B > C > D >
470 <Location /><br />
472 </Location><br />
474 <Files f.html><br />
478 <VirtualHost *><br />
479 <Directory /a/b><br />
481 </Directory><br />
482 </VirtualHost><br />
484 <DirectoryMatch "^.*b$"><br />
486 </DirectoryMatch><br />
488 <Directory /a/b><br />
490 </Directory><br />
494 <p>For a more concrete example, consider the following. Regardless of
495 any access restrictions placed in <directive module="core"
496 type="section">Directory</directive> sections, the <directive
497 module="core" type="section">Location</directive> section will be
498 evaluated last and will allow unrestricted access to the server. In
499 other words, order of merging is important, so be careful!</p>
502 <Location /><br />
503 Order deny,allow<br />
505 </Location><br />
507 # Woops! This <Directory> section will have no effect<br />
508 <Directory /><br />
509 Order allow,deny<br />
511 Deny from badguy.example.com<br />