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