]> granicus.if.org Git - apache/blob - docs/manual/sections.html.en
Help doc writer to spot places where:
[apache] / docs / manual / sections.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
5 <!--
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7               This file is generated from xml source: DO NOT EDIT
8         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
9       -->
10 <title>Configuration Sections - Apache HTTP Server Version 2.5</title>
11 <link href="./style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="./style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <link href="./style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="./style/css/prettify.css" />
14 <script src="./style/scripts/prettify.min.js" type="text/javascript">
15 </script>
16
17 <link href="./images/favicon.ico" rel="shortcut icon" /></head>
18 <body id="manual-page"><div id="page-header">
19 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p>
20 <p class="apache">Apache HTTP Server Version 2.5</p>
21 <img alt="" src="./images/feather.png" /></div>
22 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div>
23 <div id="path">
24 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="./">Version 2.5</a></div><div id="page-content"><div id="preamble"><h1>Configuration Sections</h1>
25 <div class="toplang">
26 <p><span>Available Languages: </span><a href="./en/sections.html" title="English">&nbsp;en&nbsp;</a> |
27 <a href="./fr/sections.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
28 <a href="./ja/sections.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
29 <a href="./ko/sections.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
30 <a href="./tr/sections.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
31 </div>
32  <p>Directives in the <a href="configuring.html">configuration files</a> may apply to the
33 entire server, or they may be restricted to apply only to particular
34 directories, files, hosts, or URLs.  This document describes how to
35 use configuration section containers or <code>.htaccess</code> files
36 to change the scope of other configuration directives.</p>
37 </div>
38 <div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#types">Types of Configuration Section Containers</a></li>
39 <li><img alt="" src="./images/down.gif" /> <a href="#file-and-web">Filesystem, Webspace, and Boolean Expressions</a></li>
40 <li><img alt="" src="./images/down.gif" /> <a href="#virtualhost">Virtual Hosts</a></li>
41 <li><img alt="" src="./images/down.gif" /> <a href="#proxy">Proxy</a></li>
42 <li><img alt="" src="./images/down.gif" /> <a href="#whatwhere">What Directives are Allowed?</a></li>
43 <li><img alt="" src="./images/down.gif" /> <a href="#merging">How the sections are merged</a></li>
44 </ul><h3>See also</h3><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
45 <div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
46 <div class="section">
47 <h2><a name="types" id="types">Types of Configuration Section Containers</a> <a title="Permanent link" href="#types" class="permalink">&para;</a></h2>
48
49 <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/core.html">core</a></code></li><li><code class="module"><a href="./mod/mod_version.html">mod_version</a></code></li><li><code class="module"><a href="./mod/mod_proxy.html">mod_proxy</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#directorymatch">&lt;DirectoryMatch&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#filesmatch">&lt;FilesMatch&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#if">&lt;If&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#ifdefine">&lt;IfDefine&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#ifmodule">&lt;IfModule&gt;</a></code></li><li><code class="directive"><a href="./mod/mod_version.html#ifversion">&lt;IfVersion&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#locationmatch">&lt;LocationMatch&gt;</a></code></li><li><code class="directive"><a href="./mod/mod_md.html#mdomainsetsection">&lt;MDomainSet&gt;</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxy">&lt;Proxy&gt;</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxymatch">&lt;ProxyMatch&gt;</a></code></li><li><code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code></li></ul></td></tr></table>
50
51 <p>There are two basic types of containers.  Most containers are
52 evaluated for each request.  The enclosed directives are applied only
53 for those requests that match the containers.  The <code class="directive"><a href="./mod/core.html#ifdefine">&lt;IfDefine&gt;</a></code>, <code class="directive"><a href="./mod/core.html#ifmodule">&lt;IfModule&gt;</a></code>, and
54 <code class="directive"><a href="./mod/mod_version.html#ifversion">&lt;IfVersion&gt;</a></code>
55 containers, on the other hand, are evaluated only at server startup
56 and restart.  If their conditions are true at startup, then the
57 enclosed directives will apply to all requests.  If the conditions are
58 not true, the enclosed directives will be ignored.</p>
59
60 <p>The <code class="directive"><a href="./mod/core.html#ifdefine">&lt;IfDefine&gt;</a></code> directive
61 encloses directives that will only be applied if an appropriate
62 parameter is defined on the <code class="program"><a href="./programs/httpd.html">httpd</a></code> command line.  For example,
63 with the following configuration, all requests will be redirected
64 to another site only if the server is started using
65 <code>httpd -DClosedForNow</code>:</p>
66
67 <pre class="prettyprint lang-config">&lt;IfDefine ClosedForNow&gt;
68     Redirect "/" "http://otherserver.example.com/"
69 &lt;/IfDefine&gt;</pre>
70
71
72 <p>The <code class="directive"><a href="./mod/core.html#ifmodule">&lt;IfModule&gt;</a></code>
73 directive is very similar, except it encloses directives that will
74 only be applied if a particular module is available in the server.
75 The module must either be statically compiled in the server, or it
76 must be dynamically compiled and its <code class="directive"><a href="./mod/mod_so.html#loadmodule">LoadModule</a></code> line must be earlier in the
77 configuration file.  This directive should only be used if you need
78 your configuration file to work whether or not certain modules are
79 installed.  It should not be used to enclose directives that you want
80 to work all the time, because it can suppress useful error messages
81 about missing modules.</p>
82
83 <p>In the following example, the <code class="directive"><a href="./mod/mod_mime_magic.html#mimemagicfile">MimeMagicFile</a></code> directive will be
84 applied only if <code class="module"><a href="./mod/mod_mime_magic.html">mod_mime_magic</a></code> is available.</p>
85
86 <pre class="prettyprint lang-config">&lt;IfModule mod_mime_magic.c&gt;
87     MimeMagicFile "conf/magic"
88 &lt;/IfModule&gt;</pre>
89
90
91 <p>The <code class="directive"><a href="./mod/mod_version.html#ifversion">&lt;IfVersion&gt;</a></code>
92 directive is very similar to <code class="directive"><a href="./mod/core.html#ifdefine">&lt;IfDefine&gt;</a></code> and <code class="directive"><a href="./mod/core.html#ifmodule">&lt;IfModule&gt;</a></code>, except it encloses directives that will
93 only be applied if a particular version of the server is executing.  This
94 module is designed for the use in test suites and large networks which have to
95 deal with different httpd versions and different configurations.</p>
96
97 <pre class="prettyprint lang-config">&lt;IfVersion &gt;= 2.4&gt;
98     # this happens only in versions greater or
99     # equal 2.4.0.
100 &lt;/IfVersion&gt;</pre>
101
102
103 <p><code class="directive"><a href="./mod/core.html#ifdefine">&lt;IfDefine&gt;</a></code>,
104 <code class="directive"><a href="./mod/core.html#ifmodule">&lt;IfModule&gt;</a></code>, and the
105 <code class="directive"><a href="./mod/mod_version.html#ifversion">&lt;IfVersion&gt;</a></code>
106 can apply negative conditions by preceding their test with "!".
107 Also, these sections can be nested to achieve more complex
108 restrictions.</p>
109 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
110 <div class="section">
111 <h2><a name="file-and-web" id="file-and-web">Filesystem, Webspace, and Boolean Expressions</a> <a title="Permanent link" href="#file-and-web" class="permalink">&para;</a></h2>
112
113 <p>The most commonly used configuration section containers are the
114 ones that change the configuration of particular places in the
115 filesystem or webspace.  First, it is important to understand the
116 difference between the two.  The filesystem is the view of your disks
117 as seen by your operating system.  For example, in a default install,
118 Apache httpd resides at <code>/usr/local/apache2</code> in the Unix
119 filesystem or <code>"c:/Program Files/Apache Group/Apache2"</code> in
120 the Windows filesystem.  (Note that forward slashes should always be
121 used as the path separator in Apache httpd configuration files, even for Windows.)  In contrast,
122 the webspace is the view of your site as delivered by the web server
123 and seen by the client.  So the path <code>/dir/</code> in the
124 webspace corresponds to the path
125 <code>/usr/local/apache2/htdocs/dir/</code> in the filesystem of a
126 default Apache httpd install on Unix.  The webspace need not map directly to
127 the filesystem, since webpages may be generated dynamically
128 from databases or other locations.</p>
129
130 <h3><a name="filesystem" id="filesystem">Filesystem Containers</a></h3>
131
132 <p>The <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>
133 and <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code>
134 directives, along with their <a class="glossarylink" href="./glossary.html#regex" title="see glossary">regex</a>
135 counterparts, apply directives to
136 parts of the filesystem.  Directives enclosed in a <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> section apply to
137 the named filesystem directory and all subdirectories of that
138 directory (as well as the files in those directories).
139 The same effect can be obtained using <a href="howto/htaccess.html">.htaccess files</a>.  For example, in the
140 following configuration, directory indexes will be enabled for the
141 <code>/var/web/dir1</code> directory and all subdirectories.</p>
142
143 <pre class="prettyprint lang-config">&lt;Directory "/var/web/dir1"&gt;
144     Options +Indexes
145 &lt;/Directory&gt;</pre>
146
147
148 <p>Directives enclosed in a <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code> section apply to any file with
149 the specified name, regardless of what directory it lies in.
150 So for example, the following configuration directives will,
151 when placed in the main section of the configuration file,
152 deny access to any file named <code>private.html</code> regardless
153 of where it is found.</p>
154
155 <pre class="prettyprint lang-config">&lt;Files "private.html"&gt;
156     Require all denied
157 &lt;/Files&gt;</pre>
158
159
160 <p>To address files found in a particular part of the filesystem, the
161 <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code> and
162 <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> sections
163 can be combined.  For example, the following configuration will deny
164 access to <code>/var/web/dir1/private.html</code>,
165 <code>/var/web/dir1/subdir2/private.html</code>,
166 <code>/var/web/dir1/subdir3/private.html</code>, and any other instance
167 of <code>private.html</code> found under the <code>/var/web/dir1/</code>
168 directory.</p>
169
170 <pre class="prettyprint lang-config">&lt;Directory "/var/web/dir1"&gt;
171     &lt;Files "private.html"&gt;
172         Require all denied
173     &lt;/Files&gt;
174 &lt;/Directory&gt;</pre>
175
176
177
178 <h3><a name="webspace" id="webspace">Webspace Containers</a></h3>
179
180 <p>The <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>
181 directive and its <a class="glossarylink" href="./glossary.html#regex" title="see glossary">regex</a> counterpart, on
182 the other hand, change the
183 configuration for content in the webspace.  For example, the following
184 configuration prevents access to any URL-path that begins in /private.
185 In particular, it will apply to requests for
186 <code>http://yoursite.example.com/private</code>,
187 <code>http://yoursite.example.com/private123</code>, and
188 <code>http://yoursite.example.com/private/dir/file.html</code> as well
189 as any other requests starting with the <code>/private</code> string.</p>
190
191 <pre class="prettyprint lang-config">&lt;LocationMatch "^/private"&gt;
192     Require all denied
193 &lt;/LocationMatch&gt;</pre>
194
195
196 <p>The <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>
197 directive need not have anything to do with the filesystem.
198 For example, the following example shows how to map a particular
199 URL to an internal Apache HTTP Server handler provided by <code class="module"><a href="./mod/mod_status.html">mod_status</a></code>.
200 No file called <code>server-status</code> needs to exist in the
201 filesystem.</p>
202
203 <pre class="prettyprint lang-config">&lt;Location "/server-status"&gt;
204     SetHandler server-status
205 &lt;/Location&gt;</pre>
206
207
208
209 <h3><a name="overlapping-webspace" id="overlapping-webspace">Overlapping Webspace</a></h3>
210 <p>In order to have two overlapping URLs one has to consider the order in which
211 certain sections or directives are evaluated. For
212 <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code> this would be:</p>
213 <pre class="prettyprint lang-config">&lt;Location "/foo"&gt;
214 &lt;/Location&gt;
215 &lt;Location "/foo/bar"&gt;
216 &lt;/Location&gt;</pre>
217
218 <p><code class="directive"><a href="./mod/mod_alias.html#alias">&lt;Alias&gt;</a></code>es on the other hand,
219 are mapped vice-versa:</p>
220 <pre class="prettyprint lang-config">Alias "/foo/bar" "/srv/www/uncommon/bar"
221 Alias "/foo"     "/srv/www/common/foo"</pre>
222
223 <p>The same is true for the <code class="directive"><a href="./mod/mod_proxy.html#proxypass">ProxyPass</a></code>
224 directives:</p>
225 <pre class="prettyprint lang-config">ProxyPass "/special-area" "http://special.example.com" smax=5 max=10
226 ProxyPass "/" "balancer://mycluster/" stickysession=JSESSIONID|jsessionid nofailover=On</pre>
227
228
229
230 <h3><a name="wildcards" id="wildcards">Wildcards and Regular Expressions</a></h3>
231
232 <p>The <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>,
233 <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code>, and
234 <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>
235 directives can each use shell-style wildcard characters as in
236 <code>fnmatch</code> from the C standard library.  The character "*"
237 matches any sequence of characters, "?" matches any single character,
238 and "[<em>seq</em>]" matches any character in <em>seq</em>.  The "/"
239 character will not be matched by any wildcard; it must be specified
240 explicitly.</p>
241
242 <p>If even more flexible matching is required, each
243 container has a regular expression (regex) counterpart <code class="directive"><a href="./mod/core.html#directorymatch">&lt;DirectoryMatch&gt;</a></code>, <code class="directive"><a href="./mod/core.html#filesmatch">&lt;FilesMatch&gt;</a></code>, and <code class="directive"><a href="./mod/core.html#locationmatch">&lt;LocationMatch&gt;</a></code> that allow
244 perl-compatible
245 <a class="glossarylink" href="./glossary.html#regex" title="see glossary">regular expressions</a>
246 to be used in choosing the matches.  But see the section below on
247 configuration merging to find out how using regex sections will change
248 how directives are applied.</p>
249
250 <p>A non-regex wildcard section that changes the configuration of
251 all user directories could look as follows:</p>
252
253 <pre class="prettyprint lang-config">&lt;Directory "/home/*/public_html"&gt;
254     Options Indexes
255 &lt;/Directory&gt;</pre>
256
257
258 <p>Using regex sections, we can deny access to many types of image files
259 at once:</p>
260 <pre class="prettyprint lang-config">&lt;FilesMatch "\.(?i:gif|jpe?g|png)$"&gt;
261     Require all denied
262 &lt;/FilesMatch&gt;</pre>
263
264
265 <p>Regular expressions containing <strong>named groups and
266 backreferences</strong> are added to the environment with the
267 corresponding name in uppercase. This allows elements of filename paths
268 and URLs to be referenced from within <a href="expr.html">expressions</a>
269 and modules like <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code>.</p>
270
271 <pre class="prettyprint lang-config">&lt;DirectoryMatch "^/var/www/combined/(?&lt;SITENAME&gt;[^/]+)"&gt;
272     require ldap-group "cn=%{env:MATCH_SITENAME},ou=combined,o=Example"
273 &lt;/DirectoryMatch&gt;</pre>
274
275
276
277
278 <h3><a name="expressions" id="expressions">Boolean expressions</a></h3>
279 <p>The <code class="directive"><a href="./mod/core.html#if">&lt;If&gt;</a></code>
280 directive change the configuration depending on a condition which can be
281 expressed by a boolean expression. For example, the following configuration
282 denies access if the HTTP Referer header does not start with
283 "http://www.example.com/".</p>
284 <pre class="prettyprint lang-config">&lt;If "!(%{HTTP_REFERER} -strmatch 'http://www.example.com/*')"&gt;
285     Require all denied
286 &lt;/If&gt;</pre>
287
288
289
290
291 <h3><a name="whichwhen" id="whichwhen">What to use When</a></h3>
292
293 <p>Choosing between filesystem containers and webspace containers is
294 actually quite easy.  When applying directives to objects that reside
295 in the filesystem always use <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> or <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code>.  When applying directives to objects
296 that do not reside in the filesystem (such as a webpage generated from
297 a database), use <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>.</p>
298
299 <p>It is important to never use <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code> when trying to restrict
300 access to objects in the filesystem.  This is because many
301 different webspace locations (URLs) could map to the same filesystem
302 location, allowing your restrictions to be circumvented.
303 For example, consider the following configuration:</p>
304
305 <pre class="prettyprint lang-config">&lt;Location "/dir/"&gt;
306     Require all denied
307 &lt;/Location&gt;</pre>
308
309
310 <p>This works fine if the request is for
311 <code>http://yoursite.example.com/dir/</code>.  But what if you are on
312 a case-insensitive filesystem?  Then your restriction could be easily
313 circumvented by requesting
314 <code>http://yoursite.example.com/DIR/</code>.  The <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> directive, in
315 contrast, will apply to any content served from that location,
316 regardless of how it is called.  (An exception is filesystem links.
317 The same directory can be placed in more than one part of the
318 filesystem using symbolic links.  The <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> directive will follow the symbolic
319 link without resetting the pathname.  Therefore, for the highest level
320 of security, symbolic links should be disabled with the appropriate
321 <code class="directive"><a href="./mod/core.html#options">Options</a></code> directive.)</p>
322
323 <p>If you are, perhaps, thinking that none of this applies to you
324 because you use a case-sensitive filesystem, remember that there are
325 many other ways to map multiple webspace locations to the same
326 filesystem location.  Therefore you should always use the filesystem
327 containers when you can.  There is, however, one exception to this
328 rule.  Putting configuration restrictions in a <code>&lt;Location
329 "/"&gt;</code> section is perfectly safe because this section will apply
330 to all requests regardless of the specific URL.</p>
331
332
333 <h3><a name="nesting" id="nesting">Nesting of sections</a></h3>
334
335 <p>Some section types can be nested inside other section types. On the one
336 hand, <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code> can be used
337 inside <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>.  On
338 the other hand, <code class="directive"><a href="./mod/core.html#if">&lt;If&gt;</a></code> can
339 be used inside <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>,
340 <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>, and <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code> sections (but not inside another
341 <code class="directive"><a href="./mod/core.html#if">&lt;If&gt;</a></code>). The regex
342 counterparts of the named section behave identically.</p>
343
344 <p>Nested sections are merged after non-nested sections of the same type.</p>
345
346
347
348 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
349 <div class="section">
350 <h2><a name="virtualhost" id="virtualhost">Virtual Hosts</a> <a title="Permanent link" href="#virtualhost" class="permalink">&para;</a></h2>
351
352 <p>The <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code>
353 container encloses directives that apply to specific hosts.
354 This is useful when serving multiple hosts from the same machine
355 with a different configuration for each.  For more information,
356 see the <a href="vhosts/">Virtual Host Documentation</a>.</p>
357 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
358 <div class="section">
359 <h2><a name="proxy" id="proxy">Proxy</a> <a title="Permanent link" href="#proxy" class="permalink">&para;</a></h2>
360
361 <p>The <code class="directive"><a href="./mod/mod_proxy.html#proxy">&lt;Proxy&gt;</a></code>
362 and <code class="directive"><a href="./mod/mod_proxy.html#proxymatch">&lt;ProxyMatch&gt;</a></code>
363 containers apply enclosed configuration directives only
364 to sites accessed through <code class="module"><a href="./mod/mod_proxy.html">mod_proxy</a></code>'s proxy server
365 that match the specified URL. For example, the following configuration
366 will allow only a subset of clients to access the
367 <code>www.example.com</code> website using the proxy server:</p>
368
369 <pre class="prettyprint lang-config">&lt;Proxy "http://www.example.com/*"&gt;
370     Require host yournetwork.example.com
371 &lt;/Proxy&gt;</pre>
372
373 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
374 <div class="section">
375 <h2><a name="whatwhere" id="whatwhere">What Directives are Allowed?</a> <a title="Permanent link" href="#whatwhere" class="permalink">&para;</a></h2>
376
377 <p>To find out what directives are allowed in what types of
378 configuration sections, check the <a href="mod/directive-dict.html#Context">Context</a> of the directive.
379 Everything that is allowed in
380 <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>
381 sections is also syntactically allowed in
382 <code class="directive"><a href="./mod/core.html#directorymatch">&lt;DirectoryMatch&gt;</a></code>,
383 <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code>,
384 <code class="directive"><a href="./mod/core.html#filesmatch">&lt;FilesMatch&gt;</a></code>,
385 <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>,
386 <code class="directive"><a href="./mod/core.html#locationmatch">&lt;LocationMatch&gt;</a></code>,
387 <code class="directive"><a href="./mod/mod_proxy.html#proxy">&lt;Proxy&gt;</a></code>,
388 and <code class="directive"><a href="./mod/mod_proxy.html#proxymatch">&lt;ProxyMatch&gt;</a></code>
389 sections.  There are some exceptions, however:</p>
390
391 <ul>
392 <li>The <code class="directive"><a href="./mod/core.html#allowoverride">AllowOverride</a></code> directive
393 works only in <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>
394 sections.</li>
395
396 <li>The <code>FollowSymLinks</code> and
397 <code>SymLinksIfOwnerMatch</code> <code class="directive"><a href="./mod/core.html#options">Options</a></code> work only in <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> sections or
398 <code>.htaccess</code> files.</li>
399
400 <li>The <code class="directive"><a href="./mod/core.html#options">Options</a></code> directive cannot
401 be used in <code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code>
402 and <code class="directive"><a href="./mod/core.html#filesmatch">&lt;FilesMatch&gt;</a></code>
403 sections.</li>
404 </ul>
405 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
406 <div class="section">
407 <h2><a name="merging" id="merging">How the sections are merged</a> <a title="Permanent link" href="#merging" class="permalink">&para;</a></h2>
408
409 <p>The configuration sections are applied in a very particular order.
410 Since this can have important effects on how configuration directives
411 are interpreted, it is important to understand how this works.</p>
412
413     <p>The order of merging is:</p>
414
415     <ol>
416       <li> <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> (except regular expressions)
417       and <code>.htaccess</code> done simultaneously (with
418       <code>.htaccess</code>, if allowed, overriding
419       <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>)</li>
420
421       <li><code class="directive"><a href="./mod/core.html#directorymatch">&lt;DirectoryMatch&gt;</a></code>
422       (and <code>&lt;Directory "~"&gt;</code>)</li>
423
424       <li><code class="directive"><a href="./mod/core.html#files">&lt;Files&gt;</a></code> and <code class="directive"><a href="./mod/core.html#filesmatch">&lt;FilesMatch&gt;</a></code> done
425       simultaneously</li>
426
427       <li><code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code>
428       and <code class="directive"><a href="./mod/core.html#locationmatch">&lt;LocationMatch&gt;</a></code> done simultaneously</li>
429
430       <li><code class="directive"><a href="./mod/core.html#if">&lt;If&gt;</a></code>
431       </li>
432
433     </ol>
434
435     <p>Some important remarks:</p>
436     <ul>
437         <li>Apart from <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>, within each group the sections are
438         processed in the order they appear in the configuration files.
439         For example, a request for <em>/foo/bar</em> will match
440         <code>&lt;Location "/foo/bar"&gt;</code> and 
441         <code>&lt;Location "/foo"&gt;</code> (group 4 in this case):
442         both sections will be evaluated
443         but in the order they appear in the configuration files.</li>
444         <li><code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code>
445         (group 1 above) is processed in the order shortest directory
446         component to longest. For example,
447         <code>&lt;Directory "/var/web/dir"&gt;</code> will be processed before
448         <code>&lt;Directory "/var/web/dir/subdir"&gt;</code>.</li>
449         <li>If multiple <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> sections apply
450         to the same directory they are processed in the configuration file
451         order.</li>
452         <li>Configurations included via the <code class="directive"><a href="./mod/core.html#include">Include</a></code> directive will be treated as if
453         they were inside the including file at the location of the
454         <code class="directive"><a href="./mod/core.html#include">Include</a></code> directive.</li>
455         <li>Sections inside <code class="directive"><a href="./mod/core.html#virtualhost">&lt;VirtualHost&gt;</a></code> sections
456         are applied <em>after</em> the corresponding sections outside
457         the virtual host definition. This allows virtual hosts to
458         override the main server configuration.</li>
459         <li>When the request is served by <code class="module"><a href="./mod/mod_proxy.html">mod_proxy</a></code>, the
460         <code class="directive"><a href="./mod/mod_proxy.html#proxy">&lt;Proxy&gt;</a></code>
461         container takes the place of the <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> container in the processing
462         order.</li>
463     </ul>
464
465     <div class="note"><h3>Technical Note</h3>
466       There is actually a
467       <code>&lt;Location&gt;</code>/<code>&lt;LocationMatch&gt;</code>
468       sequence performed just before the name translation phase
469       (where <code>Aliases</code> and <code>DocumentRoots</code>
470       are used to map URLs to filenames). The results of this
471       sequence are completely thrown away after the translation has
472       completed.
473     </div>
474
475 <h3><a name="relationship-module-configuration" id="relationship-module-configuration">Relationship between modules and configuration sections</a></h3>
476     <p>One question that often arises after reading how configuration sections are
477     merged is related to how and when directives of specific modules like <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code>
478     are processed. The answer is not trivial and needs a bit of background. 
479     Each httpd module manages its own configuration, and each of its directives in httpd.conf specify one piece 
480     of configuration in a particular context. httpd does not execute a command as it is read.</p>
481     <p>At runtime, the core of httpd iterates over the defined configuration sections in the order
482     described above to determine which ones apply to the current request. When the first section matches, 
483     it is considered the current configuration for this request. If a subsequent section matches too, 
484     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
485     are evaluated.</p>
486     <p>After the above step, the "real" processing of the HTTP request begins: each module has a chance to run 
487     and perform whatever tasks they like. They can retrieve their own final merged configuration from the core
488     of the httpd to determine how they should act.</p>
489     <p>An example can help to visualize the whole process. The following configuration uses the 
490         <code class="directive"><a href="./mod/mod_headers.html#header">Header</a></code> directive of <code class="module"><a href="./mod/mod_headers.html">mod_headers</a></code> to set
491         a specific HTTP header. What value will httpd set in the <code>CustomHeaderName</code> header for a request to
492         <code>/example/index.html</code> ?
493     </p>
494     <pre class="prettyprint lang-config">&lt;Directory "/"&gt;
495     Header set CustomHeaderName one
496     &lt;FilesMatch ".*"&gt;
497         Header set CustomHeaderName three
498     &lt;/FilesMatch&gt;
499 &lt;/Directory&gt;
500
501 &lt;Directory "/example"&gt;
502     Header set CustomHeaderName two
503 &lt;/Directory&gt;</pre>
504     
505     <ul>
506         <li><code class="directive">Directory</code> "/" matches and an initial configuration to set the <code>CustomHeaderName</code> header with the value <code>one</code> is created.</li>
507         <li><code class="directive">Directory</code> "/example" matches, and since <code class="module"><a href="./mod/mod_headers.html">mod_headers</a></code> 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>
508         <li><code class="directive">FilesMatch</code> ".*" matches and another merge opportunity arises, causing the <code>CustomHeaderName</code> header to be set with the value <code>three</code>.</li>
509         <li>Eventually during the next steps of the HTTP request processing <code class="module"><a href="./mod/mod_headers.html">mod_headers</a></code> will be called and it will receive the configuration to set the <code>CustomHeaderName</code> header with the value <code>three</code>. <code class="module"><a href="./mod/mod_headers.html">mod_headers</a></code> 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>
510     </ul>
511
512     <p>This is true for .htaccess too since they have the same priority as <code class="directive">Directory</code> in the merge order. The important concept to understand is that configuration sections like  <code class="directive">Directory</code> and <code class="directive">FilesMatch</code> are not comparable to module specific directives like <code class="directive"><a href="./mod/mod_headers.html#header">Header</a></code> or <code class="directive"><a href="./mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> because they operate on different levels.
513     </p>
514
515
516 <h3><a name="merge-examples" id="merge-examples">Some useful examples</a></h3>
517
518 <p>Below is an artificial example to show the order of
519 merging. Assuming they all apply to the request, the directives in
520 this example will be applied in the order A &gt; B &gt; C &gt; D &gt;
521 E.</p>
522
523 <pre class="prettyprint lang-config">&lt;Location "/"&gt;
524     E
525 &lt;/Location&gt;
526
527 &lt;Files "f.html"&gt;
528     D
529 &lt;/Files&gt;
530
531 &lt;VirtualHost *&gt;
532     &lt;Directory "/a/"&gt;
533         B
534     &lt;/Directory&gt;
535 &lt;/VirtualHost&gt;
536
537 &lt;DirectoryMatch "^.*b$"&gt;
538     C
539 &lt;/DirectoryMatch&gt;
540
541 &lt;Directory "/a/b"&gt;
542     A
543 &lt;/Directory&gt;</pre>
544
545
546
547 <p>For a more concrete example, consider the following.  Regardless of
548 any access restrictions placed in <code class="directive"><a href="./mod/core.html#directory">&lt;Directory&gt;</a></code> sections, the <code class="directive"><a href="./mod/core.html#location">&lt;Location&gt;</a></code> section will be
549 evaluated last and will allow unrestricted access to the server.  In
550 other words, order of merging is important, so be careful!</p>
551
552 <pre class="prettyprint lang-config">&lt;Location "/"&gt;
553     Require all granted
554 &lt;/Location&gt;
555
556 # Whoops!  This &lt;Directory&gt; section will have no effect
557 &lt;Directory "/"&gt;
558     &lt;RequireAll&gt;
559         Require all granted
560         Require not host badguy.example.com
561     &lt;/RequireAll&gt;
562 &lt;/Directory&gt;</pre>
563
564
565
566
567 </div></div>
568 <div class="bottomlang">
569 <p><span>Available Languages: </span><a href="./en/sections.html" title="English">&nbsp;en&nbsp;</a> |
570 <a href="./fr/sections.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
571 <a href="./ja/sections.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
572 <a href="./ko/sections.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
573 <a href="./tr/sections.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
574 </div><div class="top"><a href="#page-header"><img src="./images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
575 <script type="text/javascript"><!--//--><![CDATA[//><!--
576 var comments_shortname = 'httpd';
577 var comments_identifier = 'http://httpd.apache.org/docs/trunk/sections.html';
578 (function(w, d) {
579     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
580         d.write('<div id="comments_thread"><\/div>');
581         var s = d.createElement('script');
582         s.type = 'text/javascript';
583         s.async = true;
584         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
585         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
586     }
587     else {
588         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
589     }
590 })(window, document);
591 //--><!]]></script></div><div id="footer">
592 <p class="apache">Copyright 2019 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
593 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
594 if (typeof(prettyPrint) !== 'undefined') {
595     prettyPrint();
596 }
597 //--><!]]></script>
598 </body></html>