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 <example>Alias /docs /var/web</example>
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 <example>ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+)
138 /home/$1/cgi-bin/$2</example>
140 <p>will map a request to
141 <code>http://example.com/~user/cgi-bin/script.cgi</code> to the
142 path <code>/home/user/cgi-bin/script.cgi</code> and will treat
143 the resulting file as a CGI script.</p>
146 <section id="user"><title>User Directories</title>
148 <p>Traditionally on Unix systems, the home directory of a
149 particular <em>user</em> can be referred to as
150 <code>~user/</code>. The module <module>mod_userdir</module>
151 extends this idea to the web by allowing files under each user's
152 home directory to be accessed using URLs such as the
155 <example>http://www.example.com/~user/file.html</example>
157 <p>For security reasons, it is inappropriate to give direct
158 access to a user's home directory from the web. Therefore, the
159 <directive module="mod_userdir">UserDir</directive> directive
160 specifies a directory underneath the user's home directory
161 where web files are located. Using the default setting of
162 <code>Userdir public_html</code>, the above URL maps to a file
164 <code>/home/user/public_html/file.html</code> where
165 <code>/home/user/</code> is the user's home directory as
166 specified in <code>/etc/passwd</code>.</p>
168 <p>There are also several other forms of the
169 <code>Userdir</code> directive which you can use on systems
170 where <code>/etc/passwd</code> does not contain the location of
171 the home directory.</p>
173 <p>Some people find the "~" symbol (which is often encoded on the
174 web as <code>%7e</code>) to be awkward and prefer to use an
175 alternate string to represent user directories. This functionality
176 is not supported by mod_userdir. However, if users' home
177 directories are structured in a regular way, then it is possible
178 to use the <directive module="mod_alias">AliasMatch</directive>
179 directive to achieve the desired effect. For example, to make
180 <code>http://www.example.com/upages/user/file.html</code> map to
181 <code>/home/user/public_html/file.html</code>, use the following
182 <code>AliasMatch</code> directive:</p>
184 <example>AliasMatch ^/upages/([a-zA-Z0-9]+)(/(.*))?$
185 /home/$1/public_html/$3</example>
188 <section id="redirect"><title>URL Redirection</title>
190 <p>The configuration directives discussed in the above sections
191 tell httpd to get content from a specific place in the filesystem
192 and return it to the client. Sometimes, it is desirable instead to
193 inform the client that the requested content is located at a
194 different URL, and instruct the client to make a new request with
195 the new URL. This is called <em>redirection</em> and is
196 implemented by the <directive
197 module="mod_alias">Redirect</directive> directive. For example, if
198 the contents of the directory <code>/foo/</code> under the
199 <directive module="core">DocumentRoot</directive> are moved
200 to the new directory <code>/bar/</code>, you can instruct clients
201 to request the content at the new location as follows:</p>
203 <example>Redirect permanent /foo/
204 http://www.example.com/bar/</example>
206 <p>This will redirect any URL-Path starting in
207 <code>/foo/</code> to the same URL path on the
208 <code>www.example.com</code> server with <code>/bar/</code>
209 substituted for <code>/foo/</code>. You can redirect clients to
210 any server, not only the origin server.</p>
212 <p>httpd also provides a <directive
213 module="mod_alias">RedirectMatch</directive> directive for more
214 complicated rewriting problems. For example, to redirect requests
215 for the site home page to a different site, but leave all other
216 requests alone, use the following configuration:</p>
218 <example>RedirectMatch permanent ^/$
219 http://www.example.com/startpage.html</example>
221 <p>Alternatively, to temporarily redirect all pages on one site
222 to a particular page on another site, use the following:</p>
224 <example>RedirectMatch temp .*
225 http://othersite.example.com/startpage.html</example>
228 <section id="proxy"><title>Reverse Proxy</title>
230 <p>httpd also allows you to bring remote documents into the URL space
231 of the local server. This technique is called <em>reverse
232 proxying</em> because the web server acts like a proxy server by
233 fetching the documents from a remote server and returning them to the
234 client. It is different from normal (forward) proxying because, to the client,
235 it appears the documents originate at the reverse proxy server.</p>
237 <p>In the following example, when clients request documents under the
238 <code>/foo/</code> directory, the server fetches those documents from
239 the <code>/bar/</code> directory on <code>internal.example.com</code>
240 and returns them to the client as if they were from the local
244 ProxyPass /foo/ http://internal.example.com/bar/<br />
245 ProxyPassReverse /foo/ http://internal.example.com/bar/<br />
246 ProxyPassReverseCookieDomain internal.example.com public.example.com<br />
247 ProxyPassReverseCookiePath /foo/ /bar/
250 <p>The <directive module="mod_proxy">ProxyPass</directive> configures
251 the server to fetch the appropriate documents, while the
252 <directive module="mod_proxy">ProxyPassReverse</directive>
253 directive rewrites redirects originating at
254 <code>internal.example.com</code> so that they target the appropriate
255 directory on the local server. Similarly, the
256 <directive module="mod_proxy">ProxyPassReverseCookieDomain</directive>
257 and <directive module="mod_proxy">ProxyPassReverseCookiePath</directive>
258 rewrite cookies set by the backend server.</p>
259 <p>It is important to note, however, that
260 links inside the documents will not be rewritten. So any absolute
261 links on <code>internal.example.com</code> will result in the client
262 breaking out of the proxy server and requesting directly from
263 <code>internal.example.com</code>. You can modify these links (and other
264 content) in a page as it is being served to the client using
265 <module>mod_substitute</module>.</p>
268 Substitute s/internal\.example\.com/www.example.com/i
271 <p>For more sophisticated rewriting of links in HTML and XHTML, the
272 <module>mod_proxy_html</module> module is also available. It allows you
273 to create maps of URLs that need to be rewritten, so that comples
274 proxying scenarios can be handled.</p>
277 <section id="rewrite"><title>Rewriting Engine</title>
279 <p>When even more powerful substitution is required, the rewriting
280 engine provided by <module>mod_rewrite</module>
281 can be useful. The directives provided by this module can use
282 characteristics of the request such as browser type or source IP
283 address in deciding from where to serve content. In addition,
284 mod_rewrite can use external database files or programs to
285 determine how to handle a request. The rewriting engine is capable
286 of performing all three types of mappings discussed above:
287 internal redirects (aliases), external redirects, and proxying.
288 Many practical examples employing mod_rewrite are discussed in the
289 <a href="rewrite/">detailed mod_rewrite documentation</a>.</p>
292 <section id="notfound"><title>File Not Found</title>
294 <p>Inevitably, URLs will be requested for which no matching
295 file can be found in the filesystem. This can happen for
296 several reasons. In some cases, it can be a result of moving
297 documents from one location to another. In this case, it is
298 best to use <a href="#redirect">URL redirection</a> to inform
299 clients of the new location of the resource. In this way, you
300 can assure that old bookmarks and links will continue to work,
301 even though the resource is at a new location.</p>
303 <p>Another common cause of "File Not Found" errors is
304 accidental mistyping of URLs, either directly in the browser,
305 or in HTML links. httpd provides the module
306 <module>mod_speling</module> (sic) to help with
307 this problem. When this module is activated, it will intercept
308 "File Not Found" errors and look for a resource with a similar
309 filename. If one such file is found, mod_speling will send an
310 HTTP redirect to the client informing it of the correct
311 location. If several "close" files are found, a list of
312 available alternatives will be presented to the client.</p>
314 <p>An especially useful feature of mod_speling, is that it will
315 compare filenames without respect to case. This can help
316 systems where users are unaware of the case-sensitive nature of
317 URLs and the unix filesystem. But using mod_speling for
318 anything more than the occasional URL correction can place
319 additional load on the server, since each "incorrect" request
320 is followed by a URL redirection and a new request from the
323 <p><module>mod_dir</module> provides <directive module="mod_dir"
324 >FallbackResource</directive>, which can be used to map virtual
325 URIs to a real resource, which then serves them. This is a very
326 useful replace to <module>mod_rewrite</module> when implementing
327 a 'front controler'</p>
329 <p>If all attempts to locate the content fail, httpd returns
330 an error page with HTTP status code 404 (file not found). The
331 appearance of this page is controlled with the
332 <directive module="core">ErrorDocument</directive> directive
333 and can be customized in a flexible manner as discussed in the
334 <a href="custom-error.html">Custom error responses</a>
338 <section id="other"><title>Other URL Mapping Modules</title>
340 <!-- TODO Flesh out each of the items in the list below. -->
342 <p>Other modules available for URL mapping include:</p>
345 <li><module>mod_actions</module> - Maps a request to a CGI script
346 based on the request method, or resource MIME type.</li>
347 <li><module>mod_dir</module> - Provides basic mapping of a trailing
348 slash into an index file such as <code>index.html</code>.</li>
349 <li><module>mod_imagemap</module> - Maps a request to a URL based
350 on where a user clicks on an image embedded in a HTML document.</li>
351 <li><module>mod_negotiation</module> - Selects an appropriate
352 document based on client preferences such as language or content