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="urlmapping.xml.meta">
25 <title>Mapping URLs to Filesystem Locations</title>
28 <p>This document explains how the Apache HTTP Server uses the URL of a request
29 to determine the filesystem location from which to serve a
33 <section id="related"><title>Related Modules and Directives</title>
37 <module>mod_actions</module>
38 <module>mod_alias</module>
39 <module>mod_dir</module>
40 <module>mod_imagemap</module>
41 <module>mod_negotiation</module>
42 <module>mod_proxy</module>
43 <module>mod_rewrite</module>
44 <module>mod_speling</module>
45 <module>mod_userdir</module>
46 <module>mod_vhost_alias</module>
49 <directive module="mod_alias">Alias</directive>
50 <directive module="mod_alias">AliasMatch</directive>
51 <directive module="mod_speling">CheckSpelling</directive>
52 <directive module="core">DocumentRoot</directive>
53 <directive module="core">ErrorDocument</directive>
54 <directive module="core">Options</directive>
55 <directive module="mod_proxy">ProxyPass</directive>
56 <directive module="mod_proxy">ProxyPassReverse</directive>
57 <directive module="mod_proxy">ProxyPassReverseCookieDomain</directive>
58 <directive module="mod_proxy">ProxyPassReverseCookiePath</directive>
59 <directive module="mod_alias">Redirect</directive>
60 <directive module="mod_alias">RedirectMatch</directive>
61 <directive module="mod_rewrite">RewriteCond</directive>
62 <directive module="mod_rewrite">RewriteRule</directive>
63 <directive module="mod_alias">ScriptAlias</directive>
64 <directive module="mod_alias">ScriptAliasMatch</directive>
65 <directive module="mod_userdir">UserDir</directive>
70 <section id="documentroot"><title>DocumentRoot</title>
72 <p>In deciding what file to serve for a given request, httpd's
73 default behavior is to take the URL-Path for the request (the part
74 of the URL following the hostname and port) and add it to the end
75 of the <directive module="core">DocumentRoot</directive> specified
76 in your configuration files. Therefore, the files and directories
77 underneath the <directive module="core">DocumentRoot</directive>
78 make up the basic document tree which will be visible from the
81 <p>For example, if <directive module="core">DocumentRoot</directive>
82 were set to <code>/var/www/html</code> then a request for
83 <code>http://www.example.com/fish/guppies.html</code> would result
84 in the file <code>/var/www/html/fish/guppies.html</code> being
85 served to the requesting client.</p>
87 <p>httpd is also capable of <a href="vhosts/">Virtual
88 Hosting</a>, where the server receives requests for more than one
89 host. In this case, a different <directive
90 module="core">DocumentRoot</directive> can be specified for each
91 virtual host, or alternatively, the directives provided by the
92 module <module>mod_vhost_alias</module> can
93 be used to dynamically determine the appropriate place from which
94 to serve content based on the requested IP address or
97 <p>The <directive module="core">DocumentRoot</directive> directive
98 is set in your main server configuration file
99 (<code>httpd.conf</code>) and, possibly, once per additional <a
100 href="vhosts/">Virtual Host</a> you create.</p>
103 <section id="outside"><title>Files Outside the DocumentRoot</title>
105 <p>There are frequently circumstances where it is necessary to
106 allow web access to parts of the filesystem that are not strictly
107 underneath the <directive
108 module="core">DocumentRoot</directive>. httpd offers several
109 different ways to accomplish this. On Unix systems, symbolic links
110 can bring other parts of the filesystem under the <directive
111 module="core">DocumentRoot</directive>. For security reasons,
112 httpd will follow symbolic links only if the <directive
113 module="core">Options</directive> setting for the relevant
114 directory includes <code>FollowSymLinks</code> or
115 <code>SymLinksIfOwnerMatch</code>.</p>
117 <p>Alternatively, the <directive
118 module="mod_alias">Alias</directive> directive will map any part
119 of the filesystem into the web space. For example, with</p>
121 <highlight language="config">Alias /docs /var/web</highlight>
123 <p>the URL <code>http://www.example.com/docs/dir/file.html</code>
124 will be served from <code>/var/web/dir/file.html</code>. The
125 <directive module="mod_alias">ScriptAlias</directive> directive
126 works the same way, with the additional effect that all content
127 located at the target path is treated as <glossary ref="cgi"
128 >CGI</glossary> scripts.</p>
130 <p>For situations where you require additional flexibility, you
131 can use the <directive module="mod_alias">AliasMatch</directive>
132 and <directive module="mod_alias">ScriptAliasMatch</directive>
133 directives to do powerful <glossary ref="regex">regular
134 expression</glossary> based matching and substitution. For
137 <highlight language="config">
138 ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+) /home/$1/cgi-bin/$2
141 <p>will map a request to
142 <code>http://example.com/~user/cgi-bin/script.cgi</code> to the
143 path <code>/home/user/cgi-bin/script.cgi</code> and will treat
144 the resulting file as a CGI script.</p>
147 <section id="user"><title>User Directories</title>
149 <p>Traditionally on Unix systems, the home directory of a
150 particular <em>user</em> can be referred to as
151 <code>~user/</code>. The module <module>mod_userdir</module>
152 extends this idea to the web by allowing files under each user's
153 home directory to be accessed using URLs such as the
156 <example>http://www.example.com/~user/file.html</example>
158 <p>For security reasons, it is inappropriate to give direct
159 access to a user's home directory from the web. Therefore, the
160 <directive module="mod_userdir">UserDir</directive> directive
161 specifies a directory underneath the user's home directory
162 where web files are located. Using the default setting of
163 <code>Userdir public_html</code>, the above URL maps to a file
165 <code>/home/user/public_html/file.html</code> where
166 <code>/home/user/</code> is the user's home directory as
167 specified in <code>/etc/passwd</code>.</p>
169 <p>There are also several other forms of the
170 <code>Userdir</code> directive which you can use on systems
171 where <code>/etc/passwd</code> does not contain the location of
172 the home directory.</p>
174 <p>Some people find the "~" symbol (which is often encoded on the
175 web as <code>%7e</code>) to be awkward and prefer to use an
176 alternate string to represent user directories. This functionality
177 is not supported by mod_userdir. However, if users' home
178 directories are structured in a regular way, then it is possible
179 to use the <directive module="mod_alias">AliasMatch</directive>
180 directive to achieve the desired effect. For example, to make
181 <code>http://www.example.com/upages/user/file.html</code> map to
182 <code>/home/user/public_html/file.html</code>, use the following
183 <code>AliasMatch</code> directive:</p>
185 <highlight language="config">
186 AliasMatch ^/upages/([a-zA-Z0-9]+)(/(.*))?$ /home/$1/public_html/$3
190 <section id="redirect"><title>URL Redirection</title>
192 <p>The configuration directives discussed in the above sections
193 tell httpd to get content from a specific place in the filesystem
194 and return it to the client. Sometimes, it is desirable instead to
195 inform the client that the requested content is located at a
196 different URL, and instruct the client to make a new request with
197 the new URL. This is called <em>redirection</em> and is
198 implemented by the <directive
199 module="mod_alias">Redirect</directive> directive. For example, if
200 the contents of the directory <code>/foo/</code> under the
201 <directive module="core">DocumentRoot</directive> are moved
202 to the new directory <code>/bar/</code>, you can instruct clients
203 to request the content at the new location as follows:</p>
205 <highlight language="config">
206 Redirect permanent /foo/ http://www.example.com/bar/
209 <p>This will redirect any URL-Path starting in
210 <code>/foo/</code> to the same URL path on the
211 <code>www.example.com</code> server with <code>/bar/</code>
212 substituted for <code>/foo/</code>. You can redirect clients to
213 any server, not only the origin server.</p>
215 <p>httpd also provides a <directive
216 module="mod_alias">RedirectMatch</directive> directive for more
217 complicated rewriting problems. For example, to redirect requests
218 for the site home page to a different site, but leave all other
219 requests alone, use the following configuration:</p>
221 <highlight language="config">
222 RedirectMatch permanent ^/$ http://www.example.com/startpage.html
225 <p>Alternatively, to temporarily redirect all pages on one site
226 to a particular page on another site, use the following:</p>
228 <highlight language="config">
229 RedirectMatch temp .* http://othersite.example.com/startpage.html
233 <section id="proxy"><title>Reverse Proxy</title>
235 <p>httpd also allows you to bring remote documents into the URL space
236 of the local server. This technique is called <em>reverse
237 proxying</em> because the web server acts like a proxy server by
238 fetching the documents from a remote server and returning them to the
239 client. It is different from normal (forward) proxying because, to the client,
240 it appears the documents originate at the reverse proxy server.</p>
242 <p>In the following example, when clients request documents under the
243 <code>/foo/</code> directory, the server fetches those documents from
244 the <code>/bar/</code> directory on <code>internal.example.com</code>
245 and returns them to the client as if they were from the local
248 <highlight language="config">
249 ProxyPass /foo/ http://internal.example.com/bar/<br />
250 ProxyPassReverse /foo/ http://internal.example.com/bar/<br />
251 ProxyPassReverseCookieDomain internal.example.com public.example.com<br />
252 ProxyPassReverseCookiePath /foo/ /bar/
255 <p>The <directive module="mod_proxy">ProxyPass</directive> configures
256 the server to fetch the appropriate documents, while the
257 <directive module="mod_proxy">ProxyPassReverse</directive>
258 directive rewrites redirects originating at
259 <code>internal.example.com</code> so that they target the appropriate
260 directory on the local server. Similarly, the
261 <directive module="mod_proxy">ProxyPassReverseCookieDomain</directive>
262 and <directive module="mod_proxy">ProxyPassReverseCookiePath</directive>
263 rewrite cookies set by the backend server.</p>
264 <p>It is important to note, however, that
265 links inside the documents will not be rewritten. So any absolute
266 links on <code>internal.example.com</code> will result in the client
267 breaking out of the proxy server and requesting directly from
268 <code>internal.example.com</code>. You can modify these links (and other
269 content) in a page as it is being served to the client using
270 <module>mod_substitute</module>.</p>
272 <highlight language="config">
273 Substitute s/internal\.example\.com/www.example.com/i
276 <p>For more sophisticated rewriting of links in HTML and XHTML, the
277 <module>mod_proxy_html</module> module is also available. It allows you
278 to create maps of URLs that need to be rewritten, so that complex
279 proxying scenarios can be handled.</p>
282 <section id="rewrite"><title>Rewriting Engine</title>
284 <p>When even more powerful substitution is required, the rewriting
285 engine provided by <module>mod_rewrite</module>
286 can be useful. The directives provided by this module can use
287 characteristics of the request such as browser type or source IP
288 address in deciding from where to serve content. In addition,
289 mod_rewrite can use external database files or programs to
290 determine how to handle a request. The rewriting engine is capable
291 of performing all three types of mappings discussed above:
292 internal redirects (aliases), external redirects, and proxying.
293 Many practical examples employing mod_rewrite are discussed in the
294 <a href="rewrite/">detailed mod_rewrite documentation</a>.</p>
297 <section id="notfound"><title>File Not Found</title>
299 <p>Inevitably, URLs will be requested for which no matching
300 file can be found in the filesystem. This can happen for
301 several reasons. In some cases, it can be a result of moving
302 documents from one location to another. In this case, it is
303 best to use <a href="#redirect">URL redirection</a> to inform
304 clients of the new location of the resource. In this way, you
305 can assure that old bookmarks and links will continue to work,
306 even though the resource is at a new location.</p>
308 <p>Another common cause of "File Not Found" errors is
309 accidental mistyping of URLs, either directly in the browser,
310 or in HTML links. httpd provides the module
311 <module>mod_speling</module> (sic) to help with
312 this problem. When this module is activated, it will intercept
313 "File Not Found" errors and look for a resource with a similar
314 filename. If one such file is found, mod_speling will send an
315 HTTP redirect to the client informing it of the correct
316 location. If several "close" files are found, a list of
317 available alternatives will be presented to the client.</p>
319 <p>An especially useful feature of mod_speling, is that it will
320 compare filenames without respect to case. This can help
321 systems where users are unaware of the case-sensitive nature of
322 URLs and the unix filesystem. But using mod_speling for
323 anything more than the occasional URL correction can place
324 additional load on the server, since each "incorrect" request
325 is followed by a URL redirection and a new request from the
328 <p><module>mod_dir</module> provides <directive module="mod_dir"
329 >FallbackResource</directive>, which can be used to map virtual
330 URIs to a real resource, which then serves them. This is a very
331 useful replace to <module>mod_rewrite</module> when implementing
332 a 'front controler'</p>
334 <p>If all attempts to locate the content fail, httpd returns
335 an error page with HTTP status code 404 (file not found). The
336 appearance of this page is controlled with the
337 <directive module="core">ErrorDocument</directive> directive
338 and can be customized in a flexible manner as discussed in the
339 <a href="custom-error.html">Custom error responses</a>
343 <section id="other"><title>Other URL Mapping Modules</title>
345 <!-- TODO Flesh out each of the items in the list below. -->
347 <p>Other modules available for URL mapping include:</p>
350 <li><module>mod_actions</module> - Maps a request to a CGI script
351 based on the request method, or resource MIME type.</li>
352 <li><module>mod_dir</module> - Provides basic mapping of a trailing
353 slash into an index file such as <code>index.html</code>.</li>
354 <li><module>mod_imagemap</module> - Maps a request to a URL based
355 on where a user clicks on an image embedded in a HTML document.</li>
356 <li><module>mod_negotiation</module> - Selects an appropriate
357 document based on client preferences such as language or content