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">If</directive>
49 <directive type="section" module="core">IfDefine</directive>
50 <directive type="section" module="core">IfModule</directive>
51 <directive type="section" module="mod_version">IfVersion</directive>
52 <directive type="section" module="core">Location</directive>
53 <directive type="section" module="core">LocationMatch</directive>
54 <directive type="section" idtype="section" module="mod_md">MDomainSet</directive>
55 <directive type="section" module="mod_proxy">Proxy</directive>
56 <directive type="section" module="mod_proxy">ProxyMatch</directive>
57 <directive type="section" module="core">VirtualHost</directive>
61 <p>There are two basic types of containers. Most containers are
62 evaluated for each request. The enclosed directives are applied only
63 for those requests that match the containers. The <directive
64 type="section" module="core">IfDefine</directive>, <directive
65 type="section" module="core">IfModule</directive>, and
66 <directive type="section" module="mod_version">IfVersion</directive>
67 containers, on the other hand, are evaluated only at server startup
68 and restart. If their conditions are true at startup, then the
69 enclosed directives will apply to all requests. If the conditions are
70 not true, the enclosed directives will be ignored.</p>
72 <p>The <directive type="section" module="core">IfDefine</directive> directive
73 encloses directives that will only be applied if an appropriate
74 parameter is defined on the <program>httpd</program> command line. For example,
75 with the following configuration, all requests will be redirected
76 to another site only if the server is started using
77 <code>httpd -DClosedForNow</code>:</p>
79 <highlight language="config">
80 <IfDefine ClosedForNow>
81 Redirect "/" "http://otherserver.example.com/"
85 <p>The <directive type="section" module="core">IfModule</directive>
86 directive is very similar, except it encloses directives that will
87 only be applied if a particular module is available in the server.
88 The module must either be statically compiled in the server, or it
89 must be dynamically compiled and its <directive
90 module="mod_so">LoadModule</directive> line must be earlier in the
91 configuration file. This directive should only be used if you need
92 your configuration file to work whether or not certain modules are
93 installed. It should not be used to enclose directives that you want
94 to work all the time, because it can suppress useful error messages
95 about missing modules.</p>
97 <p>In the following example, the <directive
98 module="mod_mime_magic">MimeMagicFile</directive> directive will be
99 applied only if <module>mod_mime_magic</module> is available.</p>
101 <highlight language="config">
102 <IfModule mod_mime_magic.c>
103 MimeMagicFile conf/magic
107 <p>The <directive type="section" module="mod_version">IfVersion</directive>
108 directive is very similar to <directive type="section"
109 module="core">IfDefine</directive> and <directive type="section"
110 module="core">IfModule</directive>, except it encloses directives that will
111 only be applied if a particular version of the server is executing. This
112 module is designed for the use in test suites and large networks which have to
113 deal with different httpd versions and different configurations.</p>
115 <highlight language="config">
116 <IfVersion >= 2.4>
117 # this happens only in versions greater or
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, Webspace, and Boolean Expressions</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 httpd 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 httpd configuration files, 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 httpd 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 (as well as the files in those directories).
159 The same effect can be obtained using <a
160 href="howto/htaccess.html">.htaccess files</a>. For example, in the
161 following configuration, directory indexes will be enabled for the
162 <code>/var/web/dir1</code> directory and all subdirectories.</p>
164 <highlight language="config">
165 <Directory "/var/web/dir1">
170 <p>Directives enclosed in a <directive type="section"
171 module="core">Files</directive> section apply to any file with
172 the specified name, regardless of what directory it lies in.
173 So for example, the following configuration directives will,
174 when placed in the main section of the configuration file,
175 deny access to any file named <code>private.html</code> regardless
176 of where it is found.</p>
178 <highlight language="config">
179 <Files "private.html">
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>
194 <highlight language="config">
195 <Directory "/var/web/dir1">
196 <Files "private.html">
203 <section id="webspace"><title>Webspace Containers</title>
205 <p>The <directive type="section" module="core">Location</directive>
206 directive and its <glossary ref="regex">regex</glossary> counterpart, on
207 the other hand, change the
208 configuration for content in the webspace. For example, the following
209 configuration prevents access to any URL-path that begins in /private.
210 In particular, it will apply to requests for
211 <code>http://yoursite.example.com/private</code>,
212 <code>http://yoursite.example.com/private123</code>, and
213 <code>http://yoursite.example.com/private/dir/file.html</code> as well
214 as any other requests starting with the <code>/private</code> string.</p>
216 <highlight language="config">
217 <LocationMatch "^/private">
219 </LocationMatch>
222 <p>The <directive type="section" module="core">Location</directive>
223 directive need not have anything to do with the filesystem.
224 For example, the following example shows how to map a particular
225 URL to an internal Apache HTTP Server handler provided by <module>mod_status</module>.
226 No file called <code>server-status</code> needs to exist in the
229 <highlight language="config">
230 <Location "/server-status">
231 SetHandler server-status
236 <section id="overlapping-webspace"><title>Overlapping Webspace</title>
237 <p>In order to have two overlapping URLs one has to consider the order in which
238 certain sections or directives are evaluated. For
239 <directive type="section" module="core">Location</directive> this would be:</p>
240 <highlight language="config">
241 <Location "/foo">
243 <Location "/foo/bar">
246 <p><directive type="section" module="mod_alias">Alias</directive>es on the other hand,
247 are mapped vice-versa:</p>
248 <highlight language="config">
249 Alias "/foo/bar" "/srv/www/uncommon/bar"
250 Alias "/foo" "/srv/www/common/foo"
252 <p>The same is true for the <directive module="mod_proxy">ProxyPass</directive>
254 <highlight language="config">
255 ProxyPass "/special-area" "http://special.example.com" smax=5 max=10
256 ProxyPass "/" "balancer://mycluster/" stickysession=JSESSIONID|jsessionid nofailover=On
260 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
262 <p>The <directive type="section" module="core">Directory</directive>,
263 <directive type="section" module="core">Files</directive>, and
264 <directive type="section" module="core">Location</directive>
265 directives can each use shell-style wildcard characters as in
266 <code>fnmatch</code> from the C standard library. The character "*"
267 matches any sequence of characters, "?" matches any single character,
268 and "[<em>seq</em>]" matches any character in <em>seq</em>. The "/"
269 character will not be matched by any wildcard; it must be specified
272 <p>If even more flexible matching is required, each
273 container has a regular expression (regex) counterpart <directive
274 type="section" module="core">DirectoryMatch</directive>, <directive
275 type="section" module="core">FilesMatch</directive>, and <directive
276 type="section" module="core">LocationMatch</directive> that allow
278 <glossary ref="regex">regular expressions</glossary>
279 to be used in choosing the matches. But see the section below on
280 configuration merging to find out how using regex sections will change
281 how directives are applied.</p>
283 <p>A non-regex wildcard section that changes the configuration of
284 all user directories could look as follows:</p>
286 <highlight language="config">
287 <Directory "/home/*/public_html">
292 <p>Using regex sections, we can deny access to many types of image files
294 <highlight language="config">
295 <FilesMatch "\.(?i:gif|jpe?g|png)$">
300 <p>Regular expressions containing <strong>named groups and
301 backreferences</strong> are added to the environment with the
302 corresponding name in uppercase. This allows elements of filename paths
303 and URLs to be referenced from within <a href="expr.html">expressions</a>
304 and modules like <module>mod_rewrite</module>.</p>
306 <highlight language="config">
307 <DirectoryMatch "^/var/www/combined/(?<SITENAME>[^/]+)">
308 require ldap-group cn=%{env:MATCH_SITENAME},ou=combined,o=Example
309 </DirectoryMatch>
314 <section id="expressions"><title>Boolean expressions</title>
315 <p>The <directive type="section" module="core">If</directive>
316 directive change the configuration depending on a condition which can be
317 expressed by a boolean expression. For example, the following configuration
318 denies access if the HTTP Referer header does not start with
319 "http://www.example.com/".</p>
320 <highlight language="config">
321 <If "!(%{HTTP_REFERER} -strmatch 'http://www.example.com/*')">
328 <section id="whichwhen"><title>What to use When</title>
330 <p>Choosing between filesystem containers and webspace containers is
331 actually quite easy. When applying directives to objects that reside
332 in the filesystem always use <directive type="section"
333 module="core">Directory</directive> or <directive type="section"
334 module="core">Files</directive>. When applying directives to objects
335 that do not reside in the filesystem (such as a webpage generated from
336 a database), use <directive type="section"
337 module="core">Location</directive>.</p>
339 <p>It is important to never use <directive type="section"
340 module="core">Location</directive> when trying to restrict
341 access to objects in the filesystem. This is because many
342 different webspace locations (URLs) could map to the same filesystem
343 location, allowing your restrictions to be circumvented.
344 For example, consider the following configuration:</p>
346 <highlight language="config">
347 <Location "/dir/">
352 <p>This works fine if the request is for
353 <code>http://yoursite.example.com/dir/</code>. But what if you are on
354 a case-insensitive filesystem? Then your restriction could be easily
355 circumvented by requesting
356 <code>http://yoursite.example.com/DIR/</code>. The <directive
357 type="section" module="core">Directory</directive> directive, in
358 contrast, will apply to any content served from that location,
359 regardless of how it is called. (An exception is filesystem links.
360 The same directory can be placed in more than one part of the
361 filesystem using symbolic links. The <directive type="section"
362 module="core">Directory</directive> directive will follow the symbolic
363 link without resetting the pathname. Therefore, for the highest level
364 of security, symbolic links should be disabled with the appropriate
365 <directive module="core">Options</directive> directive.)</p>
367 <p>If you are, perhaps, thinking that none of this applies to you
368 because you use a case-sensitive filesystem, remember that there are
369 many other ways to map multiple webspace locations to the same
370 filesystem location. Therefore you should always use the filesystem
371 containers when you can. There is, however, one exception to this
372 rule. Putting configuration restrictions in a <code><Location
373 "/"></code> section is perfectly safe because this section will apply
374 to all requests regardless of the specific URL.</p>
377 <section id="nesting"><title>Nesting of sections</title>
379 <p>Some section types can be nested inside other section types. On the one
380 hand, <directive type="section" module="core">Files</directive> can be used
381 inside <directive type="section" module="core">Directory</directive>. On
382 the other hand, <directive type="section" module="core">If</directive> can
383 be used inside <directive type="section" module="core">Directory</directive>,
384 <directive type="section" module="core">Location</directive>, and <directive
385 type="section" module="core">Files</directive> sections (but not inside another
386 <directive type="section" module="core">If</directive>). The regex
387 counterparts of the named section behave identically.</p>
389 <p>Nested sections are merged after non-nested sections of the same type.</p>
395 <section id="virtualhost"><title>Virtual Hosts</title>
397 <p>The <directive type="section" module="core">VirtualHost</directive>
398 container encloses directives that apply to specific hosts.
399 This is useful when serving multiple hosts from the same machine
400 with a different configuration for each. For more information,
401 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
404 <section id="proxy"><title>Proxy</title>
406 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
407 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
408 containers apply enclosed configuration directives only
409 to sites accessed through <module>mod_proxy</module>'s proxy server
410 that match the specified URL. For example, the following configuration
411 will allow only a subset of clients to access the
412 <code>www.example.com</code> website using the proxy server:</p>
414 <highlight language="config">
415 <Proxy http://www.example.com/*>
416 Require host yournetwork.example.com
421 <section id="whatwhere"><title>What Directives are Allowed?</title>
423 <p>To find out what directives are allowed in what types of
424 configuration sections, check the <a
425 href="mod/directive-dict.html#Context">Context</a> of the directive.
426 Everything that is allowed in
427 <directive type="section" module="core">Directory</directive>
428 sections is also syntactically allowed in
429 <directive type="section" module="core">DirectoryMatch</directive>,
430 <directive type="section" module="core">Files</directive>,
431 <directive type="section" module="core">FilesMatch</directive>,
432 <directive type="section" module="core">Location</directive>,
433 <directive type="section" module="core">LocationMatch</directive>,
434 <directive type="section" module="mod_proxy">Proxy</directive>,
435 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
436 sections. There are some exceptions, however:</p>
439 <li>The <directive module="core">AllowOverride</directive> directive
440 works only in <directive type="section" module="core">Directory</directive>
443 <li>The <code>FollowSymLinks</code> and
444 <code>SymLinksIfOwnerMatch</code> <directive
445 module="core">Options</directive> work only in <directive
446 type="section" module="core">Directory</directive> sections or
447 <code>.htaccess</code> files.</li>
449 <li>The <directive module="core">Options</directive> directive cannot
450 be used in <directive type="section" module="core">Files</directive>
451 and <directive type="section" module="core">FilesMatch</directive>
456 <section id="merging"><title>How the sections are merged</title>
458 <p>The configuration sections are applied in a very particular order.
459 Since this can have important effects on how configuration directives
460 are interpreted, it is important to understand how this works.</p>
462 <p>The order of merging is:</p>
465 <li> <directive type="section"
466 module="core">Directory</directive> (except regular expressions)
467 and <code>.htaccess</code> done simultaneously (with
468 <code>.htaccess</code>, if allowed, overriding
469 <directive type="section" module="core">Directory</directive>)</li>
471 <li><directive type="section" module="core">DirectoryMatch</directive>
472 (and <code><Directory ~></code>)</li>
474 <li><directive type="section"
475 module="core">Files</directive> and <directive
476 type="section" module="core">FilesMatch</directive> done
479 <li><directive type="section" module="core">Location</directive>
480 and <directive type="section"
481 module="core">LocationMatch</directive> done simultaneously</li>
483 <li><directive type="section" module="core">If</directive>
488 <p>Some important remarks:</p>
490 <li>Apart from <directive type="section"
491 module="core">Directory</directive>, within each group the sections are
492 processed in the order they appear in the configuration files.
493 For example, a request for <em>/foo</em> will match
494 <code><Location "/foo/bar"></code> and
495 <code><Location "/foo"></code> (group 4 in this case):
496 both sections will be evaluated
497 but in the order they appear in the configuration files.</li>
498 <li><directive type="section" module="core">Directory</directive>
499 (group 1 above) is processed in the order shortest directory
500 component to longest. For example,
501 <code><Directory "/var/web/dir"></code> will be processed before
502 <code><Directory "/var/web/dir/subdir"></code>.</li>
503 <li>If multiple <directive
504 type="section" module="core">Directory</directive> sections apply
505 to the same directory they are processed in the configuration file
507 <li>Configurations included via the <directive
508 module="core">Include</directive> directive will be treated as if
509 they were inside the including file at the location of the
510 <directive module="core">Include</directive> directive.</li>
511 <li>Sections inside <directive type="section"
512 module="core">VirtualHost</directive> sections
513 are applied <em>after</em> the corresponding sections outside
514 the virtual host definition. This allows virtual hosts to
515 override the main server configuration.</li>
516 <li>When the request is served by <module>mod_proxy</module>, the
517 <directive module="mod_proxy" type="section">Proxy</directive>
518 container takes the place of the <directive module="core"
519 type="section">Directory</directive> container in the processing
523 <note><title>Technical Note</title>
525 <code><Location></code>/<code><LocationMatch></code>
526 sequence performed just before the name translation phase
527 (where <code>Aliases</code> and <code>DocumentRoots</code>
528 are used to map URLs to filenames). The results of this
529 sequence are completely thrown away after the translation has
533 <section id="relationship-module-configuration"><title>Relationship between modules and configuration sections</title>
534 <p>One question that often arises after reading how configuration sections are
535 merged is related to how and when directives of specific modules like <module>mod_rewrite</module>
536 are processed. The answer is not trivial and needs a bit of background.
537 Each httpd module manages its own configuration, and each of its directives in httpd.conf specify one piece
538 of configuration in a particular context. httpd does not execute a command as it is read.</p>
539 <p>At runtime, the core of httpd iterates over the defined configuration sections in the order
540 described above to determine which ones apply to the current request. When the first section matches,
541 it is considered the current configuration for this request. If a subsequent section matches too,
542 then each module with a directive in either of the sections is given a chance to merge its configuration between the two sections. The result is a third configuration, and the process goes on until all the configuration sections
544 <p>After the above step, the "real" processing of the HTTP request begins: each module has a chance to run
545 and perform whatever tasks they like. They can retrieve their own final merged configuration from the core
546 of the httpd to determine how they should act.</p>
547 <p>An example can help to visualize the whole process. The following configuration uses the
548 <directive module="mod_headers">Header</directive> directive of <module>mod_headers</module> to set
549 a specific HTTP header. What value will httpd set in the <code>CustomHeaderName</code> header for a request to
550 <code>/example/index.html</code> ?
552 <highlight language="config">
554 <Directory "/">
555 Header set CustomHeaderName one
556 <FilesMatch ".*">
557 Header set CustomHeaderName three
561 <Directory "/example">
562 Header set CustomHeaderName two
567 <li><directive>Directory</directive> "/" matches and an initial configuration to set the <code>CustomHeaderName</code> header with the value <code>one</code> is created.</li>
568 <li><directive>Directory</directive> "/example" matches, and since <module>mod_headers</module> specifies in its code to override in case of a merge, a new configuration is created to set the <code>CustomHeaderName</code> header with the value <code>two</code>.</li>
569 <li><directive>FilesMatch</directive> ".*" matches and another merge opportunity arises, causing the <code>CustomHeaderName</code> header to be set with the value <code>three</code>.</li>
570 <li>Eventually during the next steps of the HTTP request processing <module>mod_headers</module> will be called and it will receive the configuration to set the <code>CustomHeaderName</code> header with the value <code>three</code>. <module>mod_headers</module> normally uses this configuration to perfom its job, namely setting the foo header. This does not mean that a module can't perform a more complex action like discarding directives because not needed or deprecated, etc..</li>
573 <p>This is true for .htaccess too since they have the same priority as <directive>Directory</directive> in the merge order. The important concept to understand is that configuration sections like <directive>Directory</directive> and <directive>FilesMatch</directive> are not comparable to module specific directives like <directive module="mod_headers">Header</directive> or <directive module="mod_rewrite">RewriteRule</directive> because they operate on different levels.
577 <section id="merge-examples"><title>Some useful examples</title>
579 <p>Below is an artificial example to show the order of
580 merging. Assuming they all apply to the request, the directives in
581 this example will be applied in the order A > B > C > D >
584 <highlight language="config">
589 <Files "f.html">
593 <VirtualHost *>
594 <Directory "/a/">
599 <DirectoryMatch "^.*b$">
601 </DirectoryMatch>
603 <Directory "/a/b">
610 <p>For a more concrete example, consider the following. Regardless of
611 any access restrictions placed in <directive module="core"
612 type="section">Directory</directive> sections, the <directive
613 module="core" type="section">Location</directive> section will be
614 evaluated last and will allow unrestricted access to the server. In
615 other words, order of merging is important, so be careful!</p>
617 <highlight language="config">
622 # Whoops! This <Directory> section will have no effect
623 <Directory "/">
626 Require not host badguy.example.com