]> granicus.if.org Git - apache/blob - docs/manual/urlmapping.xml
Update transformations
[apache] / docs / manual / urlmapping.xml
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$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <manualpage metafile="urlmapping.xml.meta">
24
25   <title>Mapping URLs to Filesystem Locations</title>
26
27   <summary>
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
30     file.</p>
31   </summary>
32
33 <section id="related"><title>Related Modules and Directives</title>
34
35 <related>
36 <modulelist>
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>
47 </modulelist>
48 <directivelist>
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>
66 </directivelist>
67 </related>
68 </section>
69
70 <section id="documentroot"><title>DocumentRoot</title>
71
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
79     web.</p>
80
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>
86
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
95     hostname.</p>
96
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>
101 </section>
102
103 <section id="outside"><title>Files Outside the DocumentRoot</title>
104
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>
116
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>
120
121 <example>Alias /docs /var/web</example>
122
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>
129
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
135     example,</p>
136
137 <example>ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+)
138       /home/$1/cgi-bin/$2</example>
139
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>
144 </section>
145
146 <section id="user"><title>User Directories</title>
147
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
153     following.</p>
154
155 <example>http://www.example.com/~user/file.html</example>
156
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
163     at a directory like
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>
167
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>
172
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>
183
184 <example>AliasMatch ^/upages/([a-zA-Z0-9]+)/?(.*)
185       /home/$1/public_html/$2</example>
186 </section>
187
188 <section id="redirect"><title>URL Redirection</title>
189
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>
202
203 <example>Redirect permanent /foo/
204       http://www.example.com/bar/</example>
205
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>
211
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>
217
218 <example>RedirectMatch permanent ^/$
219       http://www.example.com/startpage.html</example>
220
221     <p>Alternatively, to temporarily redirect all pages on one site
222     to a particular page on another site, use the following:</p>
223
224 <example>RedirectMatch temp .*
225       http://othersite.example.com/startpage.html</example>
226 </section>
227
228 <section id="proxy"><title>Reverse Proxy</title>
229
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>
236
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
241 server.</p>
242
243 <example>
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/
248 </example>
249
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>
266
267 <example>
268 Substitute s/internal\.example\.com/www.example.com/i
269 </example>
270
271 <p>Additionally, a third-party module,
272 <a href="http://apache.webthing.com/mod_proxy_html/">mod_proxy_html</a>,
273 is available to rewrite links in HTML and XHTML.</p>
274 </section>
275
276 <section id="rewrite"><title>Rewriting Engine</title>
277
278     <p>When even more powerful substitution is required, the rewriting
279     engine provided by <module>mod_rewrite</module>
280     can be useful. The directives provided by this module can use
281     characteristics of the request such as browser type or source IP
282     address in deciding from where to serve content. In addition,
283     mod_rewrite can use external database files or programs to
284     determine how to handle a request. The rewriting engine is capable
285     of performing all three types of mappings discussed above:
286     internal redirects (aliases), external redirects, and proxying.
287     Many practical examples employing mod_rewrite are discussed in the
288     <a href="rewrite/">detailed mod_rewrite documentation</a>.</p>
289 </section>
290
291 <section id="notfound"><title>File Not Found</title>
292
293     <p>Inevitably, URLs will be requested for which no matching
294     file can be found in the filesystem. This can happen for
295     several reasons. In some cases, it can be a result of moving
296     documents from one location to another. In this case, it is
297     best to use <a href="#redirect">URL redirection</a> to inform
298     clients of the new location of the resource. In this way, you
299     can assure that old bookmarks and links will continue to work,
300     even though the resource is at a new location.</p>
301
302     <p>Another common cause of "File Not Found" errors is
303     accidental mistyping of URLs, either directly in the browser,
304     or in HTML links. httpd provides the module
305     <module>mod_speling</module> (sic) to help with
306     this problem. When this module is activated, it will intercept
307     "File Not Found" errors and look for a resource with a similar
308     filename. If one such file is found, mod_speling will send an
309     HTTP redirect to the client informing it of the correct
310     location. If several "close" files are found, a list of
311     available alternatives will be presented to the client.</p>
312
313     <p>An especially useful feature of mod_speling, is that it will
314     compare filenames without respect to case. This can help
315     systems where users are unaware of the case-sensitive nature of
316     URLs and the unix filesystem. But using mod_speling for
317     anything more than the occasional URL correction can place
318     additional load on the server, since each "incorrect" request
319     is followed by a URL redirection and a new request from the
320     client.</p>
321
322     <p>If all attempts to locate the content fail, httpd returns
323     an error page with HTTP status code 404 (file not found). The
324     appearance of this page is controlled with the
325     <directive module="core">ErrorDocument</directive> directive
326     and can be customized in a flexible manner as discussed in the
327     <a href="custom-error.html">Custom error responses</a>
328     document.</p>
329 </section>
330
331 <section id="other"><title>Other URL Mapping Modules</title>
332
333 <!-- TODO Flesh out each of the items in the list below. -->
334
335     <p>Other modules available for URL mapping include:</p>
336
337     <ul>
338     <li><module>mod_actions</module> - Maps a request to a CGI script
339     based on the request method, or resource MIME type.</li>
340     <li><module>mod_dir</module> - Provides basic mapping of a trailing
341     slash into an index file such as <code>index.html</code>.</li>
342     <li><module>mod_imagemap</module> - Maps a request to a URL based
343     on where a user clicks on an image embedded in a HTML document.</li>
344     <li><module>mod_negotiation</module> - Selects an appropriate
345     document based on client preferences such as language or content
346     compression.</li>
347     </ul>
348
349 </section>
350
351 </manualpage>