]> granicus.if.org Git - apache/blob - docs/manual/urlmapping.html.en
Update transformations
[apache] / docs / manual / urlmapping.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         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>Mapping URLs to Filesystem Locations - Apache HTTP Server</title>
9 <link href="./style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="./style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="./style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="./images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.3</p>
16 <img alt="" src="./images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="./images/left.gif" /></a></div>
18 <div id="path">
19 <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.3</a></div><div id="page-content"><div id="preamble"><h1>Mapping URLs to Filesystem Locations</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="./en/urlmapping.html" title="English">&nbsp;en&nbsp;</a> |
22 <a href="./fr/urlmapping.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
23 <a href="./ja/urlmapping.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
24 <a href="./ko/urlmapping.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
25 <a href="./tr/urlmapping.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
26 </div>
27
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   </div>
32 <div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#related">Related Modules and Directives</a></li>
33 <li><img alt="" src="./images/down.gif" /> <a href="#documentroot">DocumentRoot</a></li>
34 <li><img alt="" src="./images/down.gif" /> <a href="#outside">Files Outside the DocumentRoot</a></li>
35 <li><img alt="" src="./images/down.gif" /> <a href="#user">User Directories</a></li>
36 <li><img alt="" src="./images/down.gif" /> <a href="#redirect">URL Redirection</a></li>
37 <li><img alt="" src="./images/down.gif" /> <a href="#proxy">Reverse Proxy</a></li>
38 <li><img alt="" src="./images/down.gif" /> <a href="#rewrite">Rewriting Engine</a></li>
39 <li><img alt="" src="./images/down.gif" /> <a href="#notfound">File Not Found</a></li>
40 <li><img alt="" src="./images/down.gif" /> <a href="#other">Other URL Mapping Modules</a></li>
41 </ul></div>
42 <div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
43 <div class="section">
44 <h2><a name="related" id="related">Related Modules and Directives</a></h2>
45
46 <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_actions.html">mod_actions</a></code></li><li><code class="module"><a href="./mod/mod_alias.html">mod_alias</a></code></li><li><code class="module"><a href="./mod/mod_dir.html">mod_dir</a></code></li><li><code class="module"><a href="./mod/mod_imagemap.html">mod_imagemap</a></code></li><li><code class="module"><a href="./mod/mod_negotiation.html">mod_negotiation</a></code></li><li><code class="module"><a href="./mod/mod_proxy.html">mod_proxy</a></code></li><li><code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code></li><li><code class="module"><a href="./mod/mod_speling.html">mod_speling</a></code></li><li><code class="module"><a href="./mod/mod_userdir.html">mod_userdir</a></code></li><li><code class="module"><a href="./mod/mod_vhost_alias.html">mod_vhost_alias</a></code></li></ul></td><td><ul><li><code class="directive"><a href="./mod/mod_alias.html#alias">Alias</a></code></li><li><code class="directive"><a href="./mod/mod_alias.html#aliasmatch">AliasMatch</a></code></li><li><code class="directive"><a href="./mod/mod_speling.html#checkspelling">CheckSpelling</a></code></li><li><code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code></li><li><code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code></li><li><code class="directive"><a href="./mod/core.html#options">Options</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxypass">ProxyPass</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxypassreversecookiedomain">ProxyPassReverseCookieDomain</a></code></li><li><code class="directive"><a href="./mod/mod_proxy.html#proxypassreversecookiepath">ProxyPassReverseCookiePath</a></code></li><li><code class="directive"><a href="./mod/mod_alias.html#redirect">Redirect</a></code></li><li><code class="directive"><a href="./mod/mod_alias.html#redirectmatch">RedirectMatch</a></code></li><li><code class="directive"><a href="./mod/mod_rewrite.html#rewritecond">RewriteCond</a></code></li><li><code class="directive"><a href="./mod/mod_rewrite.html#rewriterule">RewriteRule</a></code></li><li><code class="directive"><a href="./mod/mod_alias.html#scriptalias">ScriptAlias</a></code></li><li><code class="directive"><a href="./mod/mod_alias.html#scriptaliasmatch">ScriptAliasMatch</a></code></li><li><code class="directive"><a href="./mod/mod_userdir.html#userdir">UserDir</a></code></li></ul></td></tr></table>
47 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
48 <div class="section">
49 <h2><a name="documentroot" id="documentroot">DocumentRoot</a></h2>
50
51     <p>In deciding what file to serve for a given request, httpd's
52     default behavior is to take the URL-Path for the request (the part
53     of the URL following the hostname and port) and add it to the end
54     of the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> specified
55     in your configuration files. Therefore, the files and directories
56     underneath the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>
57     make up the basic document tree which will be visible from the
58     web.</p>
59
60     <p>For example, if <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>
61     were set to <code>/var/www/html</code> then a request for
62     <code>http://www.example.com/fish/guppies.html</code> would result
63     in the file <code>/var/www/html/fish/guppies.html</code> being
64     served to the requesting client.</p>
65
66     <p>httpd is also capable of <a href="vhosts/">Virtual
67     Hosting</a>, where the server receives requests for more than one
68     host. In this case, a different <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> can be specified for each
69     virtual host, or alternatively, the directives provided by the
70     module <code class="module"><a href="./mod/mod_vhost_alias.html">mod_vhost_alias</a></code> can
71     be used to dynamically determine the appropriate place from which
72     to serve content based on the requested IP address or
73     hostname.</p>
74
75     <p>The <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> directive
76     is set in your main server configuration file
77     (<code>httpd.conf</code>) and, possibly, once per additional <a href="vhosts/">Virtual Host</a> you create.</p>
78 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
79 <div class="section">
80 <h2><a name="outside" id="outside">Files Outside the DocumentRoot</a></h2>
81
82     <p>There are frequently circumstances where it is necessary to
83     allow web access to parts of the filesystem that are not strictly
84     underneath the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>. httpd offers several
85     different ways to accomplish this. On Unix systems, symbolic links
86     can bring other parts of the filesystem under the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>. For security reasons,
87     httpd will follow symbolic links only if the <code class="directive"><a href="./mod/core.html#options">Options</a></code> setting for the relevant
88     directory includes <code>FollowSymLinks</code> or
89     <code>SymLinksIfOwnerMatch</code>.</p>
90
91     <p>Alternatively, the <code class="directive"><a href="./mod/mod_alias.html#alias">Alias</a></code> directive will map any part
92     of the filesystem into the web space. For example, with</p>
93
94 <div class="example"><p><code>Alias /docs /var/web</code></p></div>
95
96     <p>the URL <code>http://www.example.com/docs/dir/file.html</code>
97     will be served from <code>/var/web/dir/file.html</code>. The
98     <code class="directive"><a href="./mod/mod_alias.html#scriptalias">ScriptAlias</a></code> directive
99     works the same way, with the additional effect that all content
100     located at the target path is treated as <a class="glossarylink" href="./glossary.html#cgi" title="see glossary">CGI</a> scripts.</p>
101
102     <p>For situations where you require additional flexibility, you
103     can use the <code class="directive"><a href="./mod/mod_alias.html#aliasmatch">AliasMatch</a></code>
104     and <code class="directive"><a href="./mod/mod_alias.html#scriptaliasmatch">ScriptAliasMatch</a></code>
105     directives to do powerful <a class="glossarylink" href="./glossary.html#regex" title="see glossary">regular
106     expression</a> based matching and substitution. For
107     example,</p>
108
109 <div class="example"><p><code>ScriptAliasMatch ^/~([a-zA-Z0-9]+)/cgi-bin/(.+)
110       /home/$1/cgi-bin/$2</code></p></div>
111
112     <p>will map a request to
113     <code>http://example.com/~user/cgi-bin/script.cgi</code> to the
114     path <code>/home/user/cgi-bin/script.cgi</code> and will treat
115     the resulting file as a CGI script.</p>
116 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
117 <div class="section">
118 <h2><a name="user" id="user">User Directories</a></h2>
119
120     <p>Traditionally on Unix systems, the home directory of a
121     particular <em>user</em> can be referred to as
122     <code>~user/</code>. The module <code class="module"><a href="./mod/mod_userdir.html">mod_userdir</a></code>
123     extends this idea to the web by allowing files under each user's
124     home directory to be accessed using URLs such as the
125     following.</p>
126
127 <div class="example"><p><code>http://www.example.com/~user/file.html</code></p></div>
128
129     <p>For security reasons, it is inappropriate to give direct
130     access to a user's home directory from the web. Therefore, the
131     <code class="directive"><a href="./mod/mod_userdir.html#userdir">UserDir</a></code> directive
132     specifies a directory underneath the user's home directory
133     where web files are located. Using the default setting of
134     <code>Userdir public_html</code>, the above URL maps to a file
135     at a directory like
136     <code>/home/user/public_html/file.html</code> where
137     <code>/home/user/</code> is the user's home directory as
138     specified in <code>/etc/passwd</code>.</p>
139
140     <p>There are also several other forms of the
141     <code>Userdir</code> directive which you can use on systems
142     where <code>/etc/passwd</code> does not contain the location of
143     the home directory.</p>
144
145     <p>Some people find the "~" symbol (which is often encoded on the
146     web as <code>%7e</code>) to be awkward and prefer to use an
147     alternate string to represent user directories. This functionality
148     is not supported by mod_userdir. However, if users' home
149     directories are structured in a regular way, then it is possible
150     to use the <code class="directive"><a href="./mod/mod_alias.html#aliasmatch">AliasMatch</a></code>
151     directive to achieve the desired effect. For example, to make
152     <code>http://www.example.com/upages/user/file.html</code> map to
153     <code>/home/user/public_html/file.html</code>, use the following
154     <code>AliasMatch</code> directive:</p>
155
156 <div class="example"><p><code>AliasMatch ^/upages/([a-zA-Z0-9]+)/?(.*)
157       /home/$1/public_html/$2</code></p></div>
158 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
159 <div class="section">
160 <h2><a name="redirect" id="redirect">URL Redirection</a></h2>
161
162     <p>The configuration directives discussed in the above sections
163     tell httpd to get content from a specific place in the filesystem
164     and return it to the client. Sometimes, it is desirable instead to
165     inform the client that the requested content is located at a
166     different URL, and instruct the client to make a new request with
167     the new URL. This is called <em>redirection</em> and is
168     implemented by the <code class="directive"><a href="./mod/mod_alias.html#redirect">Redirect</a></code> directive. For example, if
169     the contents of the directory <code>/foo/</code> under the
170     <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> are moved
171     to the new directory <code>/bar/</code>, you can instruct clients
172     to request the content at the new location as follows:</p>
173
174 <div class="example"><p><code>Redirect permanent /foo/
175       http://www.example.com/bar/</code></p></div>
176
177     <p>This will redirect any URL-Path starting in
178     <code>/foo/</code> to the same URL path on the
179     <code>www.example.com</code> server with <code>/bar/</code>
180     substituted for <code>/foo/</code>. You can redirect clients to
181     any server, not only the origin server.</p>
182
183     <p>httpd also provides a <code class="directive"><a href="./mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive for more
184     complicated rewriting problems. For example, to redirect requests
185     for the site home page to a different site, but leave all other
186     requests alone, use the following configuration:</p>
187
188 <div class="example"><p><code>RedirectMatch permanent ^/$
189       http://www.example.com/startpage.html</code></p></div>
190
191     <p>Alternatively, to temporarily redirect all pages on one site
192     to a particular page on another site, use the following:</p>
193
194 <div class="example"><p><code>RedirectMatch temp .*
195       http://othersite.example.com/startpage.html</code></p></div>
196 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
197 <div class="section">
198 <h2><a name="proxy" id="proxy">Reverse Proxy</a></h2>
199
200 <p>httpd also allows you to bring remote documents into the URL space
201 of the local server.  This technique is called <em>reverse
202 proxying</em> because the web server acts like a proxy server by
203 fetching the documents from a remote server and returning them to the
204 client.  It is different from normal (forward) proxying because, to the client,
205 it appears the documents originate at the reverse proxy server.</p>
206
207 <p>In the following example, when clients request documents under the
208 <code>/foo/</code> directory, the server fetches those documents from
209 the <code>/bar/</code> directory on <code>internal.example.com</code>
210 and returns them to the client as if they were from the local
211 server.</p>
212
213 <div class="example"><p><code>
214 ProxyPass /foo/ http://internal.example.com/bar/<br />
215 ProxyPassReverse /foo/ http://internal.example.com/bar/<br />
216 ProxyPassReverseCookieDomain internal.example.com public.example.com<br />
217 ProxyPassReverseCookiePath /foo/ /bar/
218 </code></p></div>
219
220 <p>The <code class="directive"><a href="./mod/mod_proxy.html#proxypass">ProxyPass</a></code> configures
221 the server to fetch the appropriate documents, while the
222 <code class="directive"><a href="./mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a></code>
223 directive rewrites redirects originating at
224 <code>internal.example.com</code> so that they target the appropriate
225 directory on the local server.  Similarly, the
226 <code class="directive"><a href="./mod/mod_proxy.html#proxypassreversecookiedomain">ProxyPassReverseCookieDomain</a></code>
227 and <code class="directive"><a href="./mod/mod_proxy.html#proxypassreversecookiepath">ProxyPassReverseCookiePath</a></code>
228 rewrite cookies set by the backend server.</p>
229 <p>It is important to note, however, that
230 links inside the documents will not be rewritten. So any absolute
231 links on <code>internal.example.com</code> will result in the client
232 breaking out of the proxy server and requesting directly from
233 <code>internal.example.com</code>. You can modify these links (and other
234 content) in a page as it is being served to the client using
235 <code class="module"><a href="./mod/mod_substitute.html">mod_substitute</a></code>.</p>
236
237 <div class="example"><p><code>
238 Substitute s/internal\.example\.com/www.example.com/i
239 </code></p></div>
240
241 <p>Additionally, a third-party module,
242 <a href="http://apache.webthing.com/mod_proxy_html/">mod_proxy_html</a>,
243 is available to rewrite links in HTML and XHTML.</p>
244 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
245 <div class="section">
246 <h2><a name="rewrite" id="rewrite">Rewriting Engine</a></h2>
247
248     <p>When even more powerful substitution is required, the rewriting
249     engine provided by <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code>
250     can be useful. The directives provided by this module can use
251     characteristics of the request such as browser type or source IP
252     address in deciding from where to serve content. In addition,
253     mod_rewrite can use external database files or programs to
254     determine how to handle a request. The rewriting engine is capable
255     of performing all three types of mappings discussed above:
256     internal redirects (aliases), external redirects, and proxying.
257     Many practical examples employing mod_rewrite are discussed in the
258     <a href="rewrite/">detailed mod_rewrite documentation</a>.</p>
259 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
260 <div class="section">
261 <h2><a name="notfound" id="notfound">File Not Found</a></h2>
262
263     <p>Inevitably, URLs will be requested for which no matching
264     file can be found in the filesystem. This can happen for
265     several reasons. In some cases, it can be a result of moving
266     documents from one location to another. In this case, it is
267     best to use <a href="#redirect">URL redirection</a> to inform
268     clients of the new location of the resource. In this way, you
269     can assure that old bookmarks and links will continue to work,
270     even though the resource is at a new location.</p>
271
272     <p>Another common cause of "File Not Found" errors is
273     accidental mistyping of URLs, either directly in the browser,
274     or in HTML links. httpd provides the module
275     <code class="module"><a href="./mod/mod_speling.html">mod_speling</a></code> (sic) to help with
276     this problem. When this module is activated, it will intercept
277     "File Not Found" errors and look for a resource with a similar
278     filename. If one such file is found, mod_speling will send an
279     HTTP redirect to the client informing it of the correct
280     location. If several "close" files are found, a list of
281     available alternatives will be presented to the client.</p>
282
283     <p>An especially useful feature of mod_speling, is that it will
284     compare filenames without respect to case. This can help
285     systems where users are unaware of the case-sensitive nature of
286     URLs and the unix filesystem. But using mod_speling for
287     anything more than the occasional URL correction can place
288     additional load on the server, since each "incorrect" request
289     is followed by a URL redirection and a new request from the
290     client.</p>
291
292     <p>If all attempts to locate the content fail, httpd returns
293     an error page with HTTP status code 404 (file not found). The
294     appearance of this page is controlled with the
295     <code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code> directive
296     and can be customized in a flexible manner as discussed in the
297     <a href="custom-error.html">Custom error responses</a>
298     document.</p>
299 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
300 <div class="section">
301 <h2><a name="other" id="other">Other URL Mapping Modules</a></h2>
302
303
304
305     <p>Other modules available for URL mapping include:</p>
306
307     <ul>
308     <li><code class="module"><a href="./mod/mod_actions.html">mod_actions</a></code> - Maps a request to a CGI script
309     based on the request method, or resource MIME type.</li>
310     <li><code class="module"><a href="./mod/mod_dir.html">mod_dir</a></code> - Provides basic mapping of a trailing
311     slash into an index file such as <code>index.html</code>.</li>
312     <li><code class="module"><a href="./mod/mod_imagemap.html">mod_imagemap</a></code> - Maps a request to a URL based
313     on where a user clicks on an image embedded in a HTML document.</li>
314     <li><code class="module"><a href="./mod/mod_negotiation.html">mod_negotiation</a></code> - Selects an appropriate
315     document based on client preferences such as language or content
316     compression.</li>
317     </ul>
318
319 </div></div>
320 <div class="bottomlang">
321 <p><span>Available Languages: </span><a href="./en/urlmapping.html" title="English">&nbsp;en&nbsp;</a> |
322 <a href="./fr/urlmapping.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
323 <a href="./ja/urlmapping.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
324 <a href="./ko/urlmapping.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a> |
325 <a href="./tr/urlmapping.html" hreflang="tr" rel="alternate" title="Türkçe">&nbsp;tr&nbsp;</a></p>
326 </div><div id="footer">
327 <p class="apache">Copyright 2010 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>
328 <p class="menu"><a href="./mod/">Modules</a> | <a href="./mod/directives.html">Directives</a> | <a href="./faq/">FAQ</a> | <a href="./glossary.html">Glossary</a> | <a href="./sitemap.html">Sitemap</a></p></div>
329 </body></html>