You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@httpd.apache.org by Joshua Slive <sl...@commerce.ubc.ca> on 2001/02/12 20:57:26 UTC

url mapping

For a little while, I've been thinking about putting together a "unifying"
document explaining the many mysteries of how a URL is turned into a file
in Apache.  I keep thinking "that would be a good document to have".  Then
I work on it for 30 seconds, get frustrated about all the details, and put
it away again.

Anyway, I'm attaching what I have so far to see if

- anybody thinks this is interesting;
- what I am writing is at all intelligible;
- this belongs in Apache;
- there are important topics I am missing;
- anyone has any other suggestions.

The things I can think of adding include
-trailing slash redirects
-mod_speling

Anyway, it is also available at
http://garibaldi.commerce.ubc.ca:8080/ap13/htdocs/manual/urlmapping.html
if you prefer a rendered version.

Joshua.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Mapping URLs to Filesystem Locations</TITLE>
</HEAD>

<!-- Background white, links blue (unvisited), navy (visited), red (active) -->
<BODY
 BGCOLOR="#FFFFFF"
 TEXT="#000000"
 LINK="#0000FF"
 VLINK="#000080"
 ALINK="#FF0000"
>
<!--#include virtual="header.html" -->
<h1 align="center">Mapping URLs to Filesystem Locations</h1>

<ul>
<li><a href="#documentroot">DocumentRoot</a></li>
<li><a href="#outside">Files Outside the DocumentRoot</a></li>
<li><a href="#user">User Directories</a></li>
<li><a href="#redirect">URL Redirection</a></li>
<li><a href="#rewrite">Rewrite Engine</a></li>
</ul>

<hr>

<table border="1">
<tr><td valign="top">
<strong>Related Modules</strong><br><br>

<a href="mod/mod_alias.html">mod_alias</a><br>
<a href="mod/mod_rewrite.html">mod_rewrite</a><br>
<a href="mod/mod_userdir.html">mod_userdir</a><br>
<a href="mod/mod_speling.html">mod_speling</a><br>

</td><td valign="top">
<strong>Related Directives</strong><br><br>

<A HREF="mod/mod_alias.html#alias">Alias</A><br>
<A HREF="mod/mod_alias.html#aliasmatch">AliasMatch</A><br>
<A HREF="mod/mod_speling.html#checkspelling">CheckSpelling</A><br>
<A HREF="mod/core.html#documentroot">DocumentRoot</A><br>
<A HREF="mod/core.html#errordocument">ErrorDocument</A><br>
<a href="mod/core.html#options">Options</a><br>
<A HREF="mod/mod_alias.html#redirect">Redirect</A><br>
<A HREF="mod/mod_alias.html#redirectmatch">RedirectMatch</A><br>
<A HREF="mod/mod_rewrite.html#RewriteCond">RewriteCond</A><br>
<A HREF="mod/mod_rewrite.html#RewriteRule">RewriteRule</A><br>
<A HREF="mod/mod_alias.html#scriptalias">ScriptAlias</A><br>
<A HREF="mod/mod_alias.html#scriptaliasmatch">ScriptAliasMatch</A><br>
<A HREF="mod/mod_userdir.html#userdir">UserDir</A><br>

</td></tr></table>

<h2><a name="documentroot">DocumentRoot</a></h2>

<p>In deciding what file to serve for a given request, Apache starts
by taking the URL-Path for the request (the part of the URL following
the first single slash) and adding it to the end of the
<code>DocumentRoot</code> specified in your configuration files.
Therefore, the files and directories underneath the
<code>DocumentRoot</code> make up the basic document tree which will
be visible from the web.  If the server receives requests for more
than one host, then multiple <code>DocumentRoot</code>s can be
specified using the techniques described in the <a
href="vhosts/">Virtual Hosting</a> documentation.</p>

<h2><a name="outside">Files Outside the DocumentRoot</a></h2>

<p>There are frequently circumstances where it is necessary to allow
web access to parts of the filesystem which are not strictly
underneath the <code>DocumentRoot</code>.  Apache offers several
different ways to accomplish this.  On Unix systems, symbolic links
can be used to bring other parts of the filesystem under the
<code>DocumentRoot</code>.  For security reasons, symbolic links will
only be followed if the <code>Options</code> setting
for the relevant directory includes <code>FollowSymLinks</code> or
<code>SymLinksIfOwnerMatch</code>.</p>

<p>Alternatively, the <code>Alias</code> directive can be used to map
any part of the filesystem into the web space.  For example,
<blockquote><code>
       Alias /docs /var/web/docs
</blockquote></code>
will cause Apache to look in <code>/var/web/docs</code> for
any request made with a URL-Path starting in <code>/docs</code>.
The <code>ScriptAlias</code> directive works the same way, with
the additional effect that all content located at the target
path is treated as CGI scripts.</p>

<p>For situations where additional flexibility is required, the
<code>AliasMatch</code> and <code>ScriptAliasMatch</code> directives
can do powerful <a href="misc/FAQ.html#regex">regular-expression</a>
based matching and substitution.
For example,</p>
<blockquote><code>
ScriptAliasMatch ^/~([^/]*)/cgi-bin/(.*) /home/$1/cgi-bin/$2
</code></blockquote>
<p>will map a request to
<code>http://example.com/~user/cgi-bin/script.cgi</code> to the path
<code>/home/user/cgi-bin/script.cgi</code> and will treat the resulting
file as a CGI script.</p>

<h2><a name="user">User Directories</a></h2>

<p>Traditionally on Unix systems, the home directory of a particular
<em>user</em> can be referred to as <code>~user/</code>.  The module
mod_userdir extends this idea to the web by allowing files under each
user's home directory to be accessed using URLs like</p>
<blockquote><code>http://www.example.com/~user/file.html</code></blockquote>

<p>For security reasons, it would be inappropriate to give direct
access to a user's home directory from the web.  For this reason, the
<code>UserDir</code> directive is used to specify a directory
undernearth the user's home directory where web files will be located.
Using the default setting of <code>Userdir public_html</code>, the
above URL would look for a file at a directory like
<code>/home/user/public_html/file.html</code> where the
</code>/home/user/</code> is the user's home directory as specified in
<code>/etc/passwd</code>.</p>

<p>There are also several other forms of the <code>Userdir</code>
directive which can be used on systems where <code>/etc/passwd</code>
cannot be used to find the location of the home directory.</p>

<p>Some people find the "~" symbol (which is often encoded on the web
as <code>%7e</code>) to be awkward and prefer to use an alternate
string to represent user directories.  This functionality is not
supported by mod_userdir.  However, if users' home directories are
structured in a regular way, then it is possible to use the
<code>AliasMatch</code> directive to achieve the desired effect.  For
example, to make
<code>http://www.example.com/upages/user/file.html</code> map to a
<code>/home/user/public_html/file.html</code>, the following
<code>AliasMatch</code> directive can be used.</p>

<blockquote><code>
AliasMatch  ^/upages/([^/]*)/?(.*) /home/$1/public_html/$2
</code></blockquote>

<h2><a name="redirect">URL Redirection</a></h2>

<p>The configuration directives discussed in the above sections are
used to tell Apache to get content from a specific place in the
filesystem and return it to the client.  Sometimes, it is desirable
instead to inform the client that the content being requested is
located at an different URL, and instruct the client to make a new
request with the new URL.  This is referred to as <em>redirection</em>
and is implemented by the <code>Redirect</code> directive.  For
example, if the contents of the directory <code>/foo/</code> under the
<code>DocumentRoot</code> have been moved to the new directory
<code>/bar/</code>, clients can instructed to request the content at
the new location as follows.</p>

<blockquote><code>Redirect permanent
/foo/ http://www.example.com/bar/</code></blockquote>

<p>This will redirect any URL-Path starting in <code>/foo/</code> to
the <code>/foo/</code> directory of the <code>www.example.com</code>
server.  Note that clients can be redirected to any server, not
only the origin server.</p>

<p>Apache also provides a <code>RedirectMatch</code> directive which
can be used for more complicated rewriting problems.  For example,
to redirect requests for the site home page to a different site,
but leave all other requests alone, the following configuration can
be used.</p>

<blockquote><code>
RedirectMatch ^/$ http://www.example.com/startpage.html
</code></blockquote>



<h2><a name="rewrite">Rewriting Engine</a></h2>

<p>When even more powerful substitution is required, the rewriting
engine provided by mod_rewrite can be useful.  The directives provided
by this module can use characteristics of the request such as browser
type or source IP address in deciding from where to serve content.
In addition, mod_rewrite can use external database files or programs
to determine how to handle a request.</p>




<!--#include virtual="footer.html" -->
</BODY>
</HTML>


Re: url mapping

Posted by Tony Finch <do...@dotat.at>.
Joshua Slive <sl...@commerce.ubc.ca> wrote:
>
>- anybody thinks this is interesting;
>- what I am writing is at all intelligible;
>- this belongs in Apache;

Yes, it's good.

>- there are important topics I am missing;

mod_vhost_alias

><p>In deciding what file to serve for a given request, Apache starts
>by taking the URL-Path for the request (the part of the URL following
>the first single slash) and adding it to the end of the
><code>DocumentRoot</code> specified in your configuration files.
>Therefore, the files and directories underneath the
><code>DocumentRoot</code> make up the basic document tree which will
>be visible from the web.  If the server receives requests for more
>than one host, then multiple <code>DocumentRoot</code>s can be
>specified using the techniques described in the <a
>href="vhosts/">Virtual Hosting</a> documentation.</p>

Hmm, I would say this is a bit misleading since DocumentRoot handling
happens after Alias handling etc. -- unless this is just a "little
lie" for beginners (in the manner of the Camel Book) :-)

Tony.
-- 
f.a.n.finch    fanf@covalent.net    dot@dotat.at
ROCKALL: SOUTHWEST 5 OR 6, INCREASING 7 OR GALE 8 IN NORTHWEST. MAINLY FAIR.
GOOD.