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" module="mod_proxy">Proxy</directive>
55 <directive type="section" module="mod_proxy">ProxyMatch</directive>
56 <directive type="section" module="core">VirtualHost</directive>
60 <p>There are two basic types of containers. Most containers are
61 evaluated for each request. The enclosed directives are applied only
62 for those requests that match the containers. The <directive
63 type="section" module="core">IfDefine</directive>, <directive
64 type="section" module="core">IfModule</directive>, and
65 <directive type="section" module="mod_version">IfVersion</directive>
66 containers, on the other hand, are evaluated only at server startup
67 and restart. If their conditions are true at startup, then the
68 enclosed directives will apply to all requests. If the conditions are
69 not true, the enclosed directives will be ignored.</p>
71 <p>The <directive type="section" module="core">IfDefine</directive> directive
72 encloses directives that will only be applied if an appropriate
73 parameter is defined on the <program>httpd</program> command line. For example,
74 with the following configuration, all requests will be redirected
75 to another site only if the server is started using
76 <code>httpd -DClosedForNow</code>:</p>
78 <highlight language="config">
79 <IfDefine ClosedForNow>
80 Redirect "/" "http://otherserver.example.com/"
84 <p>The <directive type="section" module="core">IfModule</directive>
85 directive is very similar, except it encloses directives that will
86 only be applied if a particular module is available in the server.
87 The module must either be statically compiled in the server, or it
88 must be dynamically compiled and its <directive
89 module="mod_so">LoadModule</directive> line must be earlier in the
90 configuration file. This directive should only be used if you need
91 your configuration file to work whether or not certain modules are
92 installed. It should not be used to enclose directives that you want
93 to work all the time, because it can suppress useful error messages
94 about missing modules.</p>
96 <p>In the following example, the <directive
97 module="mod_mime_magic">MimeMagicFile</directive> directive will be
98 applied only if <module>mod_mime_magic</module> is available.</p>
100 <highlight language="config">
101 <IfModule mod_mime_magic.c>
102 MimeMagicFile conf/magic
106 <p>The <directive type="section" module="mod_version">IfVersion</directive>
107 directive is very similar to <directive type="section"
108 module="core">IfDefine</directive> and <directive type="section"
109 module="core">IfModule</directive>, except it encloses directives that will
110 only be applied if a particular version of the server is executing. This
111 module is designed for the use in test suites and large networks which have to
112 deal with different httpd versions and different configurations.</p>
114 <highlight language="config">
115 <IfVersion >= 2.4>
116 # this happens only in versions greater or
121 <p><directive type="section" module="core">IfDefine</directive>,
122 <directive type="section" module="core">IfModule</directive>, and the
123 <directive type="section" module="mod_version">IfVersion</directive>
124 can apply negative conditions by preceding their test with "!".
125 Also, these sections can be nested to achieve more complex
129 <section id="file-and-web"><title>Filesystem, Webspace, and Boolean Expressions</title>
131 <p>The most commonly used configuration section containers are the
132 ones that change the configuration of particular places in the
133 filesystem or webspace. First, it is important to understand the
134 difference between the two. The filesystem is the view of your disks
135 as seen by your operating system. For example, in a default install,
136 Apache httpd resides at <code>/usr/local/apache2</code> in the Unix
137 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
138 the Windows filesystem. (Note that forward slashes should always be
139 used as the path separator in Apache httpd configuration files, even for Windows.) In contrast,
140 the webspace is the view of your site as delivered by the web server
141 and seen by the client. So the path <code>/dir/</code> in the
142 webspace corresponds to the path
143 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
144 default Apache httpd install on Unix. The webspace need not map directly to
145 the filesystem, since webpages may be generated dynamically
146 from databases or other locations.</p>
148 <section id="filesystem"><title>Filesystem Containers</title>
150 <p>The <directive type="section" module="core">Directory</directive>
151 and <directive type="section" module="core">Files</directive>
152 directives, along with their <glossary ref="regex">regex</glossary>
153 counterparts, apply directives to
154 parts of the filesystem. Directives enclosed in a <directive
155 type="section" module="core">Directory</directive> section apply to
156 the named filesystem directory and all subdirectories of that
157 directory (as well as the files in those directories).
158 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>
163 <highlight language="config">
164 <Directory "/var/web/dir1">
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>
177 <highlight language="config">
178 <Files "private.html">
183 <p>To address files found in a particular part of the filesystem, the
184 <directive type="section" module="core">Files</directive> and
185 <directive type="section" module="core">Directory</directive> sections
186 can be combined. For example, the following configuration will deny
187 access to <code>/var/web/dir1/private.html</code>,
188 <code>/var/web/dir1/subdir2/private.html</code>,
189 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
190 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
193 <highlight language="config">
194 <Directory "/var/web/dir1">
195 <Files "private.html">
202 <section id="webspace"><title>Webspace Containers</title>
204 <p>The <directive type="section" module="core">Location</directive>
205 directive and its <glossary ref="regex">regex</glossary> counterpart, on
206 the other hand, change the
207 configuration for content in the webspace. For example, the following
208 configuration prevents access to any URL-path that begins in /private.
209 In particular, it will apply to requests for
210 <code>http://yoursite.example.com/private</code>,
211 <code>http://yoursite.example.com/private123</code>, and
212 <code>http://yoursite.example.com/private/dir/file.html</code> as well
213 as any other requests starting with the <code>/private</code> string.</p>
215 <highlight language="config">
216 <LocationMatch "^/private">
218 </LocationMatch>
221 <p>The <directive type="section" module="core">Location</directive>
222 directive need not have anything to do with the filesystem.
223 For example, the following example shows how to map a particular
224 URL to an internal Apache HTTP Server handler provided by <module>mod_status</module>.
225 No file called <code>server-status</code> needs to exist in the
228 <highlight language="config">
229 <Location "/server-status">
230 SetHandler server-status
235 <section id="overlapping-webspace"><title>Overlapping Webspace</title>
236 <p>In order to have two overlapping URLs one has to consider the order in which
237 certain sections or directives are evaluated. For
238 <directive type="section" module="core">Location</directive> this would be:</p>
239 <highlight language="config">
240 <Location "/foo">
242 <Location "/foo/bar">
245 <p><directive type="section" module="mod_alias">Alias</directive>es on the other hand,
246 are mapped vice-versa:</p>
247 <highlight language="config">
248 Alias "/foo/bar" "/srv/www/uncommon/bar"
249 Alias "/foo" "/srv/www/common/foo"
251 <p>The same is true for the <directive module="mod_proxy">ProxyPass</directive>
253 <highlight language="config">
254 ProxyPass "/special-area" "http://special.example.com" smax=5 max=10
255 ProxyPass "/" "balancer://mycluster/" stickysession=JSESSIONID|jsessionid nofailover=On
259 <section id="wildcards"><title>Wildcards and Regular Expressions</title>
261 <p>The <directive type="section" module="core">Directory</directive>,
262 <directive type="section" module="core">Files</directive>, and
263 <directive type="section" module="core">Location</directive>
264 directives can each use shell-style wildcard characters as in
265 <code>fnmatch</code> from the C standard library. The character "*"
266 matches any sequence of characters, "?" matches any single character,
267 and "[<em>seq</em>]" matches any character in <em>seq</em>. The "/"
268 character will not be matched by any wildcard; it must be specified
271 <p>If even more flexible matching is required, each
272 container has a regular expression (regex) counterpart <directive
273 type="section" module="core">DirectoryMatch</directive>, <directive
274 type="section" module="core">FilesMatch</directive>, and <directive
275 type="section" module="core">LocationMatch</directive> that allow
277 <glossary ref="regex">regular expressions</glossary>
278 to be used in choosing the matches. But see the section below on
279 configuration merging to find out how using regex sections will change
280 how directives are applied.</p>
282 <p>A non-regex wildcard section that changes the configuration of
283 all user directories could look as follows:</p>
285 <highlight language="config">
286 <Directory "/home/*/public_html">
291 <p>Using regex sections, we can deny access to many types of image files
293 <highlight language="config">
294 <FilesMatch "\.(?i:gif|jpe?g|png)$">
299 <p>Regular expressions containing <strong>named groups and
300 backreferences</strong> are added to the environment with the
301 corresponding name in uppercase. This allows elements of filename paths
302 and URLs to be referenced from within <a href="expr.html">expressions</a>
303 and modules like <module>mod_rewrite</module>.</p>
305 <highlight language="config">
306 <DirectoryMatch "^/var/www/combined/(?<SITENAME>[^/]+)">
307 require ldap-group cn=%{env:MATCH_SITENAME},ou=combined,o=Example
308 </DirectoryMatch>
313 <section id="expressions"><title>Boolean expressions</title>
314 <p>The <directive type="section" module="core">If</directive>
315 directive change the configuration depending on a condition which can be
316 expressed by a boolean expression. For example, the following configuration
317 denies access if the HTTP Referer header does not start with
318 "http://www.example.com/".</p>
319 <highlight language="config">
320 <If "!(%{HTTP_REFERER} -strmatch 'http://www.example.com/*')">
327 <section id="whichwhen"><title>What to use When</title>
329 <p>Choosing between filesystem containers and webspace containers is
330 actually quite easy. When applying directives to objects that reside
331 in the filesystem always use <directive type="section"
332 module="core">Directory</directive> or <directive type="section"
333 module="core">Files</directive>. When applying directives to objects
334 that do not reside in the filesystem (such as a webpage generated from
335 a database), use <directive type="section"
336 module="core">Location</directive>.</p>
338 <p>It is important to never use <directive type="section"
339 module="core">Location</directive> when trying to restrict
340 access to objects in the filesystem. This is because many
341 different webspace locations (URLs) could map to the same filesystem
342 location, allowing your restrictions to be circumvented.
343 For example, consider the following configuration:</p>
345 <highlight language="config">
346 <Location "/dir/">
351 <p>This works fine if the request is for
352 <code>http://yoursite.example.com/dir/</code>. But what if you are on
353 a case-insensitive filesystem? Then your restriction could be easily
354 circumvented by requesting
355 <code>http://yoursite.example.com/DIR/</code>. The <directive
356 type="section" module="core">Directory</directive> directive, in
357 contrast, will apply to any content served from that location,
358 regardless of how it is called. (An exception is filesystem links.
359 The same directory can be placed in more than one part of the
360 filesystem using symbolic links. The <directive type="section"
361 module="core">Directory</directive> directive will follow the symbolic
362 link without resetting the pathname. Therefore, for the highest level
363 of security, symbolic links should be disabled with the appropriate
364 <directive module="core">Options</directive> directive.)</p>
366 <p>If you are, perhaps, thinking that none of this applies to you
367 because you use a case-sensitive filesystem, remember that there are
368 many other ways to map multiple webspace locations to the same
369 filesystem location. Therefore you should always use the filesystem
370 containers when you can. There is, however, one exception to this
371 rule. Putting configuration restrictions in a <code><Location
372 "/"></code> section is perfectly safe because this section will apply
373 to all requests regardless of the specific URL.</p>
376 <section id="nesting"><title>Nesting of sections</title>
378 <p>Some section types can be nested inside other section types. On the one
379 hand, <directive type="section" module="core">Files</directive> can be used
380 inside <directive type="section" module="core">Directory</directive>. On
381 the other hand, <directive type="section" module="core">If</directive> can
382 be used inside <directive type="section" module="core">Directory</directive>,
383 <directive type="section" module="core">Location</directive>, and <directive
384 type="section" module="core">Files</directive> sections. The regex
385 counterparts of the named section behave identically.</p>
387 <p>Nested sections are merged after non-nested sections of the same type.</p>
393 <section id="virtualhost"><title>Virtual Hosts</title>
395 <p>The <directive type="section" module="core">VirtualHost</directive>
396 container encloses directives that apply to specific hosts.
397 This is useful when serving multiple hosts from the same machine
398 with a different configuration for each. For more information,
399 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
402 <section id="proxy"><title>Proxy</title>
404 <p>The <directive type="section" module="mod_proxy">Proxy</directive>
405 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
406 containers apply enclosed configuration directives only
407 to sites accessed through <module>mod_proxy</module>'s proxy server
408 that match the specified URL. For example, the following configuration
409 will allow only a subset of clients to access the
410 <code>www.example.com</code> website using the proxy server:</p>
412 <highlight language="config">
413 <Proxy http://www.example.com/*>
414 Require host yournetwork.example.com
419 <section id="whatwhere"><title>What Directives are Allowed?</title>
421 <p>To find out what directives are allowed in what types of
422 configuration sections, check the <a
423 href="mod/directive-dict.html#Context">Context</a> of the directive.
424 Everything that is allowed in
425 <directive type="section" module="core">Directory</directive>
426 sections is also syntactically allowed in
427 <directive type="section" module="core">DirectoryMatch</directive>,
428 <directive type="section" module="core">Files</directive>,
429 <directive type="section" module="core">FilesMatch</directive>,
430 <directive type="section" module="core">Location</directive>,
431 <directive type="section" module="core">LocationMatch</directive>,
432 <directive type="section" module="mod_proxy">Proxy</directive>,
433 and <directive type="section" module="mod_proxy">ProxyMatch</directive>
434 sections. There are some exceptions, however:</p>
437 <li>The <directive module="core">AllowOverride</directive> directive
438 works only in <directive type="section" module="core">Directory</directive>
441 <li>The <code>FollowSymLinks</code> and
442 <code>SymLinksIfOwnerMatch</code> <directive
443 module="core">Options</directive> work only in <directive
444 type="section" module="core">Directory</directive> sections or
445 <code>.htaccess</code> files.</li>
447 <li>The <directive module="core">Options</directive> directive cannot
448 be used in <directive type="section" module="core">Files</directive>
449 and <directive type="section" module="core">FilesMatch</directive>
454 <section id="merging"><title>How the sections are merged</title>
456 <p>The configuration sections are applied in a very particular order.
457 Since this can have important effects on how configuration directives
458 are interpreted, it is important to understand how this works.</p>
460 <p>The order of merging is:</p>
463 <li> <directive type="section"
464 module="core">Directory</directive> (except regular expressions)
465 and <code>.htaccess</code> done simultaneously (with
466 <code>.htaccess</code>, if allowed, overriding
467 <directive type="section" module="core">Directory</directive>)</li>
469 <li><directive type="section" module="core">DirectoryMatch</directive>
470 (and <code><Directory ~></code>)</li>
472 <li><directive type="section"
473 module="core">Files</directive> and <directive
474 type="section" module="core">FilesMatch</directive> done
477 <li><directive type="section" module="core">Location</directive>
478 and <directive type="section"
479 module="core">LocationMatch</directive> done simultaneously</li>
481 <li><directive type="section" module="core">If</directive>
486 <p>Apart from <directive type="section"
487 module="core">Directory</directive>, each group is processed in
488 the order that they appear in the configuration files. <directive
489 type="section" module="core">Directory</directive> (group 1 above)
490 is processed in the order shortest directory component to longest.
491 So for example, <code><Directory "/var/web/dir"></code> will
492 be processed before <code><Directory
493 "/var/web/dir/subdir"></code>. If multiple <directive
494 type="section" module="core">Directory</directive> sections apply
495 to the same directory they are processed in the configuration file
496 order. Configurations included via the <directive
497 module="core">Include</directive> directive will be treated as if
498 they were inside the including file at the location of the
499 <directive module="core">Include</directive> directive.</p>
501 <p>Sections inside <directive type="section"
502 module="core">VirtualHost</directive> sections
503 are applied <em>after</em> the corresponding sections outside
504 the virtual host definition. This allows virtual hosts to
505 override the main server configuration.</p>
507 <p>When the request is served by <module>mod_proxy</module>, the
508 <directive module="mod_proxy" type="section">Proxy</directive>
509 container takes the place of the <directive module="core"
510 type="section">Directory</directive> container in the processing
513 <note><title>Merge is not always Override</title>
514 <p>Later sections override earlier ones, however each module is responsible
515 for interpreting what form this override takes. A later configuration section
516 with directives from a given module might cause a conceptual "merge" of some
517 directives, all directives, or a complete replacement of the modules
518 configuration with the module defaults and directives explicitly listed in
519 the later context.</p>
520 <p><directive>Directory</directive>, <directive>FilesMatch</directive>,
521 <directive>Location</directive> and the other directives discussed in this section
522 implement the same merging strategy, namely "override" when applied to the same target.</p>
525 <note><title>Technical Note</title>
527 <code><Location></code>/<code><LocationMatch></code>
528 sequence performed just before the name translation phase
529 (where <code>Aliases</code> and <code>DocumentRoots</code>
530 are used to map URLs to filenames). The results of this
531 sequence are completely thrown away after the translation has
535 <section id="merge-examples"><title>Some Examples</title>
537 <p>Below is an artificial example to show the order of
538 merging. Assuming they all apply to the request, the directives in
539 this example will be applied in the order A > B > C > D >
542 <highlight language="config">
547 <Files "f.html">
551 <VirtualHost *>
552 <Directory "/a/emphasis role="bold"">
557 <DirectoryMatch "^.*b$">
559 </DirectoryMatch>
561 <Directory "/a/b>
567 <p>Another interesting example is the following one. As described above,
568 <directive>FilesMatch</directive> is merged after <directive>Directory</directive>,
569 therefore a request for <code>/var/www/index.html</code> will eventually get the "foo" Header set to
570 the value "two", not "three" as somebody might expect from a first look.
573 <highlight language="config">
574 <Directory "/">
576 <FilesMatch ".*">
581 <Directory "/var/www">
587 <p>The chain of values that the "foo" header will get is: "one", "three" and "two".
588 It is important to notice that <directive>Directory</directive> and <directive>FilesMatch</directive>
589 implement the same merging strategy, namely "override" when
590 applied to the same path. In this example it means that the last directive applied in the merging order,
591 <directive>FilesMatch</directive>, sets the final value for the "foo" header.
593 <p>This is true for .htaccess too, since they have the same priority as <directive>Directory</directive>
594 in the merging order.</p>
596 <p>For a more concrete example, consider the following. Regardless of
597 any access restrictions placed in <directive module="core"
598 type="section">Directory</directive> sections, the <directive
599 module="core" type="section">Location</directive> section will be
600 evaluated last and will allow unrestricted access to the server. In
601 other words, order of merging is important, so be careful!</p>
603 <highlight language="config">
608 # Whoops! This <Directory> section will have no effect
609 <Directory "/">
612 Require not host badguy.example.com