This document explains how the Apache HTTP Server uses the URL of a request to determine the filesystem location from which to serve a file.
In deciding what file to serve for a given request, httpd's
default behavior is to take the URL-Path for the request (the part
of the URL following the hostname and port) and add it to the end
of the
For example, if /var/www/html
then a request for
http://www.example.com/fish/guppies.html
would result
in the file /var/www/html/fish/guppies.html
being
served to the requesting client.
httpd is also capable of Virtual
Hosting, where the server receives requests for more than one
host. In this case, a different
The httpd.conf
) and, possibly, once per additional Virtual Host you create.
There are frequently circumstances where it is necessary to
allow web access to parts of the filesystem that are not strictly
underneath the FollowSymLinks
or
SymLinksIfOwnerMatch
.
Alternatively, the
the URL http://www.example.com/docs/dir/file.html
will be served from /var/web/dir/file.html
. The
For situations where you require additional flexibility, you
can use the
will map a request to
http://example.com/~user/cgi-bin/script.cgi
to the
path /home/user/cgi-bin/script.cgi
and will treat
the resulting file as a CGI script.
Traditionally on Unix systems, the home directory of a
particular user can be referred to as
~user/
. The module
For security reasons, it is inappropriate to give direct
access to a user's home directory from the web. Therefore, the
Userdir public_html
, the above URL maps to a file
at a directory like
/home/user/public_html/file.html
where
/home/user/
is the user's home directory as
specified in /etc/passwd
.
There are also several other forms of the
Userdir
directive which you can use on systems
where /etc/passwd
does not contain the location of
the home directory.
Some people find the "~" symbol (which is often encoded on the
web as %7e
) 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 http://www.example.com/upages/user/file.html
map to
/home/user/public_html/file.html
, use the following
AliasMatch
directive:
The configuration directives discussed in the above sections
tell httpd 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 requested content is located at a
different URL, and instruct the client to make a new request with
the new URL. This is called redirection and is
implemented by the /foo/
under the
/bar/
, you can instruct clients
to request the content at the new location as follows:
This will redirect any URL-Path starting in
/foo/
to the same URL path on the
www.example.com
server with /bar/
substituted for /foo/
. You can redirect clients to
any server, not only the origin server.
httpd also provides a
Alternatively, to temporarily redirect all pages on one site to a particular page on another site, use the following:
httpd also allows you to bring remote documents into the URL space of the local server. This technique is called reverse proxying because the web server acts like a proxy server by fetching the documents from a remote server and returning them to the client. It is different from normal (forward) proxying because, to the client, it appears the documents originate at the reverse proxy server.
In the following example, when clients request documents under the
/foo/
directory, the server fetches those documents from
the /bar/
directory on internal.example.com
and returns them to the client as if they were from the local
server.
The internal.example.com
so that they target the appropriate
directory on the local server. Similarly, the
It is important to note, however, that
links inside the documents will not be rewritten. So any absolute
links on internal.example.com
will result in the client
breaking out of the proxy server and requesting directly from
internal.example.com
. You can modify these links (and other
content) in a page as it is being served to the client using
Additionally, a third-party module, mod_proxy_html, is available to rewrite links in HTML and XHTML.
When even more powerful substitution is required, the rewriting
engine provided by
Inevitably, URLs will be requested for which no matching file can be found in the filesystem. This can happen for several reasons. In some cases, it can be a result of moving documents from one location to another. In this case, it is best to use URL redirection to inform clients of the new location of the resource. In this way, you can assure that old bookmarks and links will continue to work, even though the resource is at a new location.
Another common cause of "File Not Found" errors is
accidental mistyping of URLs, either directly in the browser,
or in HTML links. httpd provides the module
An especially useful feature of mod_speling, is that it will compare filenames without respect to case. This can help systems where users are unaware of the case-sensitive nature of URLs and the unix filesystem. But using mod_speling for anything more than the occasional URL correction can place additional load on the server, since each "incorrect" request is followed by a URL redirection and a new request from the client.
If all attempts to locate the content fail, httpd returns
an error page with HTTP status code 404 (file not found). The
appearance of this page is controlled with the
Other modules available for URL mapping include:
index.html
.