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 Apache 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_alias</module>
38 <module>mod_proxy</module>
39 <module>mod_rewrite</module>
40 <module>mod_userdir</module>
41 <module>mod_speling</module>
42 <module>mod_vhost_alias</module>
45 <directive module="mod_alias">Alias</directive>
46 <directive module="mod_alias">AliasMatch</directive>
47 <directive module="mod_speling">CheckSpelling</directive>
48 <directive module="core">DocumentRoot</directive>
49 <directive module="core">ErrorDocument</directive>
50 <directive module="core">Options</directive>
51 <directive module="mod_proxy">ProxyPass</directive>
52 <directive module="mod_proxy">ProxyPassReverse</directive>
53 <directive module="mod_proxy">ProxyPassReverseCookieDomain</directive>
54 <directive module="mod_proxy">ProxyPassReverseCookiePath</directive>
55 <directive module="mod_alias">Redirect</directive>
56 <directive module="mod_alias">RedirectMatch</directive>
57 <directive module="mod_rewrite">RewriteCond</directive>
58 <directive module="mod_rewrite">RewriteMatch</directive>
59 <directive module="mod_alias">ScriptAlias</directive>
60 <directive module="mod_alias">ScriptAliasMatch</directive>
61 <directive module="mod_userdir">UserDir</directive>
66 <section id="documentroot"><title>DocumentRoot</title>
68 <p>In deciding what file to serve for a given request, Apache's
69 default behavior is to take the URL-Path for the request (the part
70 of the URL following the hostname and port) and add it to the end
71 of the <directive module="core">DocumentRoot</directive> specified
72 in your configuration files. Therefore, the files and directories
73 underneath the <directive module="core">DocumentRoot</directive>
74 make up the basic document tree which will be visible from the
77 <p>For example, if <directive module="core">DocumentRoot</directive>
78 were set to <code>/var/www/html</code> then a request for
79 <code>http://www.example.com/fish/guppies.html</code> would result
80 in the file <code>/var/www/html/fish/guppies.html</code> being
81 served to the requesting client.</p>
83 <p>Apache is also capable of <a href="vhosts/">Virtual
84 Hosting</a>, where the server receives requests for more than one
85 host. In this case, a different <directive
86 module="core">DocumentRoot</directive> can be specified for each
87 virtual host, or alternatively, the directives provided by the
88 module <module>mod_vhost_alias</module> can
89 be used to dynamically determine the appropriate place from which
90 to serve content based on the requested IP address or
93 <p>The <directive module="core">DocumentRoot</directive> directive
94 is set in your main server configuration file
95 (<code>httpd.conf</code>) and, possibly, once per additional <a
96 href="vhosts/">Virtual Host</a> you create.</p>
99 <section id="outside"><title>Files Outside the DocumentRoot</title>
101 <p>There are frequently circumstances where it is necessary to
102 allow web access to parts of the filesystem that are not strictly
103 underneath the <directive
104 module="core">DocumentRoot</directive>. Apache offers several
105 different ways to accomplish this. On Unix systems, symbolic links
106 can bring other parts of the filesystem under the <directive
107 module="core">DocumentRoot</directive>. For security reasons,
108 Apache will follow symbolic links only if the <directive
109 module="core">Options</directive> setting for the relevant
110 directory includes <code>FollowSymLinks</code> or
111 <code>SymLinksIfOwnerMatch</code>.</p>
113 <p>Alternatively, the <directive
114 module="mod_alias">Alias</directive> directive will map any part
115 of the filesystem into the web space. For example, with</p>
117 <example>Alias /docs /var/web</example>
119 <p>the URL <code>http://www.example.com/docs/dir/file.html</code>
120 will be served from <code>/var/web/dir/file.html</code>. The
121 <directive module="mod_alias">ScriptAlias</directive> directive
122 works the same way, with the additional effect that all content
123 located at the target path is treated as <glossary ref="cgi"
124 >CGI</glossary> scripts.</p>
126 <p>For situations where you require additional flexibility, you
127 can use the <directive module="mod_alias">AliasMatch</directive>
128 and <directive module="mod_alias">ScriptAliasMatch</directive>
129 directives to do powerful <glossary ref="regex">regular
130 expression</glossary> based matching and substitution. For
133 <example>ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+)
134 /home/$1/cgi-bin/$2</example>
136 <p>will map a request to
137 <code>http://example.com/~user/cgi-bin/script.cgi</code> to the
138 path <code>/home/user/cgi-bin/script.cgi</code> and will treat
139 the resulting file as a CGI script.</p>
142 <section id="user"><title>User Directories</title>
144 <p>Traditionally on Unix systems, the home directory of a
145 particular <em>user</em> can be referred to as
146 <code>~user/</code>. The module <module>mod_userdir</module>
147 extends this idea to the web by allowing files under each user's
148 home directory to be accessed using URLs such as the
151 <example>http://www.example.com/~user/file.html</example>
153 <p>For security reasons, it is inappropriate to give direct
154 access to a user's home directory from the web. Therefore, the
155 <directive module="mod_userdir">UserDir</directive> directive
156 specifies a directory underneath the user's home directory
157 where web files are located. Using the default setting of
158 <code>Userdir public_html</code>, the above URL maps to a file
160 <code>/home/user/public_html/file.html</code> where
161 <code>/home/user/</code> is the user's home directory as
162 specified in <code>/etc/passwd</code>.</p>
164 <p>There are also several other forms of the
165 <code>Userdir</code> directive which you can use on systems
166 where <code>/etc/passwd</code> does not contain the location of
167 the home directory.</p>
169 <p>Some people find the "~" symbol (which is often encoded on the
170 web as <code>%7e</code>) to be awkward and prefer to use an
171 alternate string to represent user directories. This functionality
172 is not supported by mod_userdir. However, if users' home
173 directories are structured in a regular way, then it is possible
174 to use the <directive module="mod_alias">AliasMatch</directive>
175 directive to achieve the desired effect. For example, to make
176 <code>http://www.example.com/upages/user/file.html</code> map to
177 <code>/home/user/public_html/file.html</code>, use the following
178 <code>AliasMatch</code> directive:</p>
180 <example>AliasMatch ^/upages/([a-zA-Z0-9]+)/?(.*)
181 /home/$1/public_html/$2</example>
184 <section id="redirect"><title>URL Redirection</title>
186 <p>The configuration directives discussed in the above sections
187 tell Apache to get content from a specific place in the filesystem
188 and return it to the client. Sometimes, it is desirable instead to
189 inform the client that the requested content is located at a
190 different URL, and instruct the client to make a new request with
191 the new URL. This is called <em>redirection</em> and is
192 implemented by the <directive
193 module="mod_alias">Redirect</directive> directive. For example, if
194 the contents of the directory <code>/foo/</code> under the
195 <directive module="core">DocumentRoot</directive> are moved
196 to the new directory <code>/bar/</code>, you can instruct clients
197 to request the content at the new location as follows:</p>
199 <example>Redirect permanent /foo/
200 http://www.example.com/bar/</example>
202 <p>This will redirect any URL-Path starting in
203 <code>/foo/</code> to the same URL path on the
204 <code>www.example.com</code> server with <code>/bar/</code>
205 substituted for <code>/foo/</code>. You can redirect clients to
206 any server, not only the origin server.</p>
208 <p>Apache also provides a <directive
209 module="mod_alias">RedirectMatch</directive> directive for more
210 complicated rewriting problems. For example, to redirect requests
211 for the site home page to a different site, but leave all other
212 requests alone, use the following configuration:</p>
214 <example>RedirectMatch permanent ^/$
215 http://www.example.com/startpage.html</example>
217 <p>Alternatively, to temporarily redirect all pages on one site
218 to a particular page on another site, use the following:</p>
220 <example>RedirectMatch temp .*
221 http://othersite.example.com/startpage.html</example>
224 <section id="proxy"><title>Reverse Proxy</title>
226 <p>Apache also allows you to bring remote documents into the URL space
227 of the local server. This technique is called <em>reverse
228 proxying</em> because the web server acts like a proxy server by
229 fetching the documents from a remote server and returning them to the
230 client. It is different from normal proxying because, to the client,
231 it appears the documents originate at the reverse proxy server.</p>
233 <p>In the following example, when clients request documents under the
234 <code>/foo/</code> directory, the server fetches those documents from
235 the <code>/bar/</code> directory on <code>internal.example.com</code>
236 and returns them to the client as if they were from the local
240 ProxyPass /foo/ http://internal.example.com/bar/<br />
241 ProxyPassReverse /foo/ http://internal.example.com/bar/
242 ProxyPassReverseCookieDomain internal.example.com public.example.com
243 ProxyPassReverseCookiePath /foo/ /bar/
246 <p>The <directive module="mod_proxy">ProxyPass</directive> configures
247 the server to fetch the appropriate documents, while the
248 <directive module="mod_proxy">ProxyPassReverse</directive>
249 directive rewrites redirects originating at
250 <code>internal.example.com</code> so that they target the appropriate
251 directory on the local server. Similarly, the
252 <directive module="mod_proxy">ProxyPassReverseCookieDomain</directive>
253 and <directive module="mod_proxy">ProxyPassReverseCookiePath</directive>
254 rewrite cookies set by the backend server.</p>
255 <p>It is important to note, however, that
256 links inside the documents will not be rewritten. So any absolute
257 links on <code>internal.example.com</code> will result in the client
258 breaking out of the proxy server and requesting directly from
259 <code>internal.example.com</code>. A third-party module
260 <a href="http://apache.webthing.com/mod_proxy_html/">mod_proxy_html</a>
261 is available to rewrite links in HTML and XHTML.</p>
264 <section id="rewrite"><title>Rewriting Engine</title>
266 <p>When even more powerful substitution is required, the rewriting
267 engine provided by <module>mod_rewrite</module>
268 can be useful. The directives provided by this module use
269 characteristics of the request such as browser type or source IP
270 address in deciding from where to serve content. In addition,
271 mod_rewrite can use external database files or programs to
272 determine how to handle a request. The rewriting engine is capable
273 of performing all three types of mappings discussed above:
274 internal redirects (aliases), external redirects, and proxying.
275 Many practical examples employing mod_rewrite are discussed in the
276 <a href="misc/rewriteguide.html">URL Rewriting Guide</a>.</p>
279 <section id="notfound"><title>File Not Found</title>
281 <p>Inevitably, URLs will be requested for which no matching
282 file can be found in the filesystem. This can happen for
283 several reasons. In some cases, it can be a result of moving
284 documents from one location to another. In this case, it is
285 best to use <a href="#redirect">URL redirection</a> to inform
286 clients of the new location of the resource. In this way, you
287 can assure that old bookmarks and links will continue to work,
288 even though the resource is at a new location.</p>
290 <p>Another common cause of "File Not Found" errors is
291 accidental mistyping of URLs, either directly in the browser,
292 or in HTML links. Apache provides the module
293 <module>mod_speling</module> (sic) to help with
294 this problem. When this module is activated, it will intercept
295 "File Not Found" errors and look for a resource with a similar
296 filename. If one such file is found, mod_speling will send an
297 HTTP redirect to the client informing it of the correct
298 location. If several "close" files are found, a list of
299 available alternatives will be presented to the client.</p>
301 <p>An especially useful feature of mod_speling, is that it will
302 compare filenames without respect to case. This can help
303 systems where users are unaware of the case-sensitive nature of
304 URLs and the unix filesystem. But using mod_speling for
305 anything more than the occasional URL correction can place
306 additional load on the server, since each "incorrect" request
307 is followed by a URL redirection and a new request from the
310 <p>If all attempts to locate the content fail, Apache returns
311 an error page with HTTP status code 404 (file not found). The
312 appearance of this page is controlled with the
313 <directive module="core">ErrorDocument</directive> directive
314 and can be customized in a flexible manner as discussed in the
315 <a href="custom-error.html">Custom error responses</a>