]> granicus.if.org Git - apache/blob - docs/manual/urlmapping.html.en
update transformation
[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.1</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-project/">Documentation</a> &gt; <a href="./">Version 2.1</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">&nbsp;en&nbsp;</a> | <a href="./ja/urlmapping.html">&nbsp;ja&nbsp;</a></p>
22 </div>
23
24     <p>This document explains how Apache uses the URL of a request
25     to determine the filesystem location from which to serve a
26     file.</p>
27   </div>
28 <div id="quickview"><ul id="toc"><li><img alt="" src="./images/down.gif" /> <a href="#related">Related Modules and Directives</a></li>
29 <li><img alt="" src="./images/down.gif" /> <a href="#documentroot">DocumentRoot</a></li>
30 <li><img alt="" src="./images/down.gif" /> <a href="#outside">Files Outside the DocumentRoot</a></li>
31 <li><img alt="" src="./images/down.gif" /> <a href="#user">User Directories</a></li>
32 <li><img alt="" src="./images/down.gif" /> <a href="#redirect">URL Redirection</a></li>
33 <li><img alt="" src="./images/down.gif" /> <a href="#proxy">Reverse Proxy</a></li>
34 <li><img alt="" src="./images/down.gif" /> <a href="#rewrite">Rewriting Engine</a></li>
35 <li><img alt="" src="./images/down.gif" /> <a href="#notfound">File Not Found</a></li>
36 </ul></div>
37 <div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
38 <div class="section">
39 <h2><a name="related" id="related">Related Modules and Directives</a></h2>
40
41 <table class="related"><tr><th>Related Modules</th><th>Related Directives</th></tr><tr><td><ul><li><code class="module"><a href="./mod/mod_alias.html">mod_alias</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_userdir.html">mod_userdir</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_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_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#rewritematch">RewriteMatch</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>
42 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
43 <div class="section">
44 <h2><a name="documentroot" id="documentroot">DocumentRoot</a></h2>
45
46     <p>In deciding what file to serve for a given request, Apache's
47     default behavior is to take the URL-Path for the request (the part
48     of the URL following the hostname and port) and add it to the end
49     of the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> specified
50     in your configuration files. Therefore, the files and directories
51     underneath the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>
52     make up the basic document tree which will be visible from the
53     web.</p>
54
55     <p>Apache is also capable of <a href="vhosts/">Virtual
56     Hosting</a>, where the server receives requests for more than one
57     host. In this case, a different <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> can be specified for each
58     virtual host, or alternatively, the directives provided by the
59     module <code class="module"><a href="./mod/mod_vhost_alias.html">mod_vhost_alias</a></code> can
60     be used to dynamically determine the appropriate place from which
61     to serve content based on the requested IP address or
62     hostname.</p>
63 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
64 <div class="section">
65 <h2><a name="outside" id="outside">Files Outside the DocumentRoot</a></h2>
66
67     <p>There are frequently circumstances where it is necessary to
68     allow web access to parts of the filesystem that are not strictly
69     underneath the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>. Apache offers several
70     different ways to accomplish this. On Unix systems, symbolic links
71     can bring other parts of the filesystem under the <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code>. For security reasons,
72     Apache will follow symbolic links only if the <code class="directive"><a href="./mod/core.html#options">Options</a></code> setting for the relevant
73     directory includes <code>FollowSymLinks</code> or
74     <code>SymLinksIfOwnerMatch</code>.</p>
75
76     <p>Alternatively, the <code class="directive"><a href="./mod/mod_alias.html#alias">Alias</a></code> directive will map any part
77     of the filesystem into the web space. For example, with</p>
78
79 <div class="example"><p><code>Alias /docs /var/web</code></p></div>
80
81     <p>the URL <code>http://www.example.com/docs/dir/file.html</code>
82     will be served from <code>/var/web/dir/file.html</code>. The
83     <code class="directive"><a href="./mod/mod_alias.html#scriptalias">ScriptAlias</a></code> directive
84     works the same way, with the additional effect that all content
85     located at the target path is treated as CGI scripts.</p>
86
87     <p>For situations where you require additional flexibility, you
88     can use the <code class="directive"><a href="./mod/mod_alias.html#aliasmatch">AliasMatch</a></code> and 
89     <code class="directive"><a href="./mod/mod_alias.html#scriptaliasmatch">ScriptAliasMatch</a></code>
90     directives to do powerful regular-expression based matching and
91     substitution. For example,</p>
92
93 <div class="example"><p><code>ScriptAliasMatch ^/~([a-zA-Z0-9]*)/cgi-bin/(.*)
94       /home/$1/cgi-bin/$2</code></p></div>
95
96     <p>will map a request to
97     <code>http://example.com/~user/cgi-bin/script.cgi</code> to the
98     path <code>/home/user/cgi-bin/script.cgi</code> and will treat
99     the resulting file as a CGI script.</p>
100 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
101 <div class="section">
102 <h2><a name="user" id="user">User Directories</a></h2>
103
104     <p>Traditionally on Unix systems, the home directory of a
105     particular <em>user</em> can be referred to as
106     <code>~user/</code>. The module <code class="module"><a href="./mod/mod_userdir.html">mod_userdir</a></code>
107     extends this idea to the web by allowing files under each user's
108     home directory to be accessed using URLs such as the
109     following.</p>
110
111 <div class="example"><p><code>http://www.example.com/~user/file.html</code></p></div>
112
113     <p>For security reasons, it is inappropriate to give direct
114     access to a user's home directory from the web. Therefore, the
115     <code class="directive"><a href="./mod/mod_userdir.html#userdir">UserDir</a></code> directive
116     specifies a directory underneath the user's home directory
117     where web files are located. Using the default setting of
118     <code>Userdir public_html</code>, the above URL maps to a file
119     at a directory like
120     <code>/home/user/public_html/file.html</code> where
121     <code>/home/user/</code> is the user's home directory as
122     specified in <code>/etc/passwd</code>.</p>
123
124     <p>There are also several other forms of the
125     <code>Userdir</code> directive which you can use on systems
126     where <code>/etc/passwd</code> does not contain the location of
127     the home directory.</p>
128
129     <p>Some people find the "~" symbol (which is often encoded on the
130     web as <code>%7e</code>) to be awkward and prefer to use an
131     alternate string to represent user directories. This functionality
132     is not supported by mod_userdir. However, if users' home
133     directories are structured in a regular way, then it is possible
134     to use the <code class="directive"><a href="./mod/mod_alias.html#aliasmatch">AliasMatch</a></code>
135     directive to achieve the desired effect. For example, to make
136     <code>http://www.example.com/upages/user/file.html</code> map to
137     <code>/home/user/public_html/file.html</code>, use the following
138     <code>AliasMatch</code> directive:</p>
139
140 <div class="example"><p><code>AliasMatch ^/upages/([a-zA-Z0-9]*)/?(.*)
141       /home/$1/public_html/$2</code></p></div>
142 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
143 <div class="section">
144 <h2><a name="redirect" id="redirect">URL Redirection</a></h2>
145
146     <p>The configuration directives discussed in the above sections
147     tell Apache to get content from a specific place in the filesystem
148     and return it to the client. Sometimes, it is desirable instead to
149     inform the client that the requested content is located at a
150     different URL, and instruct the client to make a new request with
151     the new URL. This is called <em>redirection</em> and is
152     implemented by the <code class="directive"><a href="./mod/mod_alias.html#redirect">Redirect</a></code> directive. For example, if
153     the contents of the directory <code>/foo/</code> under the
154     <code class="directive"><a href="./mod/core.html#documentroot">DocumentRoot</a></code> are moved
155     to the new directory <code>/bar/</code>, you can instruct clients
156     to request the content at the new location as follows:</p>
157
158 <div class="example"><p><code>Redirect permanent /foo/
159       http://www.example.com/bar/</code></p></div>
160
161     <p>This will redirect any URL-Path starting in
162     <code>/foo/</code> to the same URL path on the
163     <code>www.example.com</code> server with <code>/bar/</code>
164     substituted for <code>/foo/</code>. You can redirect clients to
165     any server, not only the origin server.</p>
166
167     <p>Apache also provides a <code class="directive"><a href="./mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive for more
168     complicated rewriting problems. For example, to redirect requests
169     for the site home page to a different site, but leave all other
170     requests alone, use the following configuration:</p>
171
172 <div class="example"><p><code>RedirectMatch permanent ^/$
173       http://www.example.com/startpage.html</code></p></div>
174
175     <p>Alternatively, to temporarily redirect all pages on one site
176     to a particular page on another site, use the following:</p>
177
178 <div class="example"><p><code>RedirectMatch temp .*
179       http://othersite.example.com/startpage.html</code></p></div>
180 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
181 <div class="section">
182 <h2><a name="proxy" id="proxy">Reverse Proxy</a></h2>
183
184 <p>Apache also allows you to bring remote documents into the URL space
185 of the local server.  This technique is called <em>reverse
186 proxying</em> because the web server acts like a proxy server by
187 fetching the documents from a remote server and returning them to the
188 client.  It is different from normal proxying because, to the client,
189 it appears the documents originate at the reverse proxy server.</p>
190
191 <p>In the following example, when clients request documents under the
192 <code>/foo/</code> directory, the server fetches those documents from
193 the <code>/bar/</code> directory on <code>internal.example.com</code>
194 and returns them to the client as if they were from the local
195 server.</p>
196
197 <div class="example"><p><code>
198 ProxyPass /foo/ http://internal.example.com/bar/<br />
199 ProxyPassReverse /foo/ http://internal.example.com/bar/
200 </code></p></div>
201
202 <p>The <code class="directive"><a href="./mod/mod_proxy.html#proxypass">ProxyPass</a></code> configures
203 the server to fetch the appropriate documents, while the
204 <code class="directive"><a href="./mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a></code>
205 directive rewrites redirects originating at
206 <code>internal.example.com</code> so that they target the appropriate
207 directory on the local server.  It is important to note, however, that
208 links inside the documents will not be rewritten.  So any absolute
209 links on <code>internal.example.com</code> will result in the client
210 breaking out of the proxy server and requesting directly from
211 <code>internal.example.com</code>.</p>
212 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
213 <div class="section">
214 <h2><a name="rewrite" id="rewrite">Rewriting Engine</a></h2>
215
216     <p>When even more powerful substitution is required, the rewriting
217     engine provided by <code class="module"><a href="./mod/mod_rewrite.html">mod_rewrite</a></code>
218     can be useful. The directives provided by this module use
219     characteristics of the request such as browser type or source IP
220     address in deciding from where to serve content. In addition,
221     mod_rewrite can use external database files or programs to
222     determine how to handle a request. The rewriting engine is capable
223     of performing all three types of mappings discussed above:
224     internal redirects (aliases), external redirects, and proxying.
225     Many practical examples employing mod_rewrite are discussed in the
226     <a href="misc/rewriteguide.html">URL Rewriting Guide</a>.</p>
227 </div><div class="top"><a href="#page-header"><img alt="top" src="./images/up.gif" /></a></div>
228 <div class="section">
229 <h2><a name="notfound" id="notfound">File Not Found</a></h2>
230
231     <p>Inevitably, URLs will be requested for which no matching
232     file can be found in the filesystem. This can happen for
233     several reasons. In some cases, it can be a result of moving
234     documents from one location to another. In this case, it is
235     best to use <a href="#redirect">URL redirection</a> to inform
236     clients of the new location of the resource. In this way, you
237     can assure that old bookmarks and links will continue to work,
238     even though the resource is at a new location.</p>
239
240     <p>Another common cause of "File Not Found" errors is
241     accidental mistyping of URLs, either directly in the browser,
242     or in HTML links. Apache provides the module
243     <code class="module"><a href="./mod/mod_speling.html">mod_speling</a></code> (sic) to help with
244     this problem. When this module is activated, it will intercept
245     "File Not Found" errors and look for a resource with a similar
246     filename. If one such file is found, mod_speling will send an
247     HTTP redirect to the client informing it of the correct
248     location. If several "close" files are found, a list of
249     available alternatives will be presented to the client.</p>
250
251     <p>An especially useful feature of mod_speling, is that it will
252     compare filenames without respect to case. This can help
253     systems where users are unaware of the case-sensitive nature of
254     URLs and the unix filesystem. But using mod_speling for
255     anything more than the occasional URL correction can place
256     additional load on the server, since each "incorrect" request
257     is followed by a URL redirection and a new request from the
258     client.</p>
259
260     <p>If all attempts to locate the content fail, Apache returns
261     an error page with HTTP status code 404 (file not found). The
262     appearance of this page is controlled with the
263     <code class="directive"><a href="./mod/core.html#errordocument">ErrorDocument</a></code> directive
264     and can be customized in a flexible manner as discussed in the
265     <a href="custom-error.html">Custom error responses</a>
266     document.</p>
267 </div></div>
268 <div class="bottomlang">
269 <p><span>Available Languages: </span><a href="./en/urlmapping.html">&nbsp;en&nbsp;</a> | <a href="./ja/urlmapping.html">&nbsp;ja&nbsp;</a></p>
270 </div><div id="footer">
271 <p class="apache">Maintained by the <a href="http://httpd.apache.org/docs-project/">Apache HTTP Server Documentation Project</a></p>
272 <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>
273 </body></html>