rewrite_guide.html.en revision 0f6366523dec7face01fcad3eadcc1378e34e663
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<title>URL Rewriting Guide - Apache HTTP Server</title>
<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<body id="manual-page"><div id="page-header">
<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>
<p class="apache">Apache HTTP Server Version 2.3</p>
<div id="path">
<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.3</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>URL Rewriting Guide</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="/en/rewrite/rewrite_guide.html" title="English"> en </a> |
<a href="/fr/rewrite/rewrite_guide.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
</div>
<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
It describes how one can use Apache's <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
to solve typical URL-based problems with which webmasters are
commonly confronted. We give detailed descriptions on how to
solve each problem by configuring URL rewriting rulesets.</p>
<div class="warning">ATTENTION: Depending on your server configuration
it may be necessary to slightly change the examples for your
<code class="module"><a href="/mod/mod_userdir.html">mod_userdir</a></code>, etc. Or rewriting a ruleset
to fit in <code>.htaccess</code> context instead
of per-server context. Always try to understand what a
particular ruleset really does before you use it. This
avoids many problems.</div>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#setenvvars">Set Environment Variables According To URL Parts</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#redirecthome">Redirect Homedirs For Foreigners</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#time-dependent">Time-Dependent Rewriting</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#structuredhomedirs">Structured Homedirs</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#retrieve-missing-data">Retrieve Missing Data from Intranet</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#new-mime-type">New MIME-type, New Service</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#mass-virtual-hosting">Mass Virtual Hosting</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#referer-deflector">Referer-based Deflector</a></li>
<div class="section">
<h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Usually the <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
of the webserver directly relates to the URL "<code>/</code>".
But often this data is not really of top-level priority. For example,
you may wish for visitors, on first entering a site, to go to a
particular subdirectory <code>/about/</code>. This may be accomplished
using the following ruleset:</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We redirect the URL <code>/</code> to
<code>/about/</code>:
</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
</pre></div>
<p>Note that this can also be handled using the <code class="directive"><a href="/mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive:</p>
<div class="example"><p><code>
RedirectMatch ^/$ http://example.com/about/
</code></p></div>
<p>Note also that the example rewrites only the root URL. That is, it
fact changed your document root - that is, if <strong>all</strong> of
your content is in fact in that subdirectory, it is greatly preferable
to simply change your <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
directive, rather than rewriting URLs.</p>
</dd>
</dl>
<div class="section">
<h2><a name="trailingslash" id="trailingslash">Trailing Slash Problem</a></h2>
<dl>
<dt>Description:</dt>
<dd><p>The vast majority of "trailing slash" problems can be dealt
with using the techniques discussed in the <a href="http://httpd.apache.org/docs/misc/FAQ-E.html#set-servername">FAQ
entry</a>. However, occasionally, there is a need to use mod_rewrite
to handle a case where a missing trailing slash causes a URL to
fail. This can happen, for example, after a series of complex
rewrite rules.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>The solution to this subtle problem is to let the server
add the trailing slash automatically. To do this
correctly we have to use an external redirect, so the
browser correctly requests subsequent images etc. If we
only did a internal rewrite, this would only work for the
directory page, but would go wrong when any images are
included into this page with relative URLs, because the
browser would request an in-lined object. For instance, a
redirect!</p>
<p>So, to do this trick we write:</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo<strong>$</strong> foo<strong>/</strong> [<strong>R</strong>]
</pre></div>
<p>Alternately, you can put the following in a
top-level <code>.htaccess</code> file in the content directory.
But note that this creates some processing overhead.</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} <strong>-d</strong>
RewriteRule ^(.+<strong>[^/]</strong>)$ $1<strong>/</strong> [R]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="setenvvars" id="setenvvars">Set Environment Variables According To URL Parts</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Perhaps you want to keep status information between
requests and use the URL to encode it. But you don't want
to use a CGI wrapper for all pages just to strip out this
information.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We use a rewrite rule to strip out the status information
and remember it via an environment variable which can be
later dereferenced from within XSSI or CGI. This way a
<code>STATUS</code> is set to the value "java".</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule ^(.*)/<strong>S=([^/]+)</strong>/(.*) $1/$3 [E=<strong>STATUS:$2</strong>]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="redirecthome" id="redirecthome">Redirect Homedirs For Foreigners</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We want to redirect homedir URLs to another webserver
does not stay in the local domain
virtual host contexts.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Just a rewrite condition:</p>
<div class="example"><pre>
RewriteEngine on
RewriteCond %{REMOTE_HOST} <strong>!^.+\.ourdomain\.com$</strong>
RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="redirectanchors" id="redirectanchors">Redirecting Anchors</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>By default, redirecting to an HTML anchor doesn't work,
because mod_rewrite escapes the <code>#</code> character,
turning it into <code>%23</code>. This, in turn, breaks the
redirection.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Use the <code>[NE]</code> flag on the
<code>RewriteRule</code>. NE stands for No Escape.
</p>
</dd>
</dl>
<div class="section">
<h2><a name="time-dependent" id="time-dependent">Time-Dependent Rewriting</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>When tricks like time-dependent content should happen a
lot of webmasters still use CGI scripts which do for
instance redirects to specialized pages. How can it be done
</dd>
<dt>Solution:</dt>
<dd>
<p>There are a lot of variables named <code>TIME_xxx</code>
for rewrite conditions. In conjunction with the special
lexicographic comparison patterns <code><STRING</code>,
<code>>STRING</code> and <code>=STRING</code> we can
do time-dependent redirects:</p>
<div class="example"><pre>
RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^foo\.html$ foo.day.html
RewriteRule ^foo\.html$ foo.night.html
</pre></div>
<code>07:01-18:59</code> and at the remaining time the
feature for a homepage...</p>
<div class="warning"><code class="module"><a href="/mod/mod_cache.html">mod_cache</a></code>, intermediate proxies
and browsers may each cache responses and cause the either page to be
shown outside of the time-window configured.
<code class="module"><a href="/mod/mod_expires.html">mod_expires</a></code> may be used to control this
effect.</div>
</dd>
</dl>
<div class="section">
<h2><a name="structuredhomedirs" id="structuredhomedirs">Structured Homedirs</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Some sites with thousands of users use a
subdirectory which begins (for instance) with the first
</dd>
<dt>Solution:</dt>
<dd>
<p>We use the following ruleset to expand the tilde URLs
into the above layout.</p>
<div class="example"><pre>
RewriteEngine on
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="dynamic-mirror" id="dynamic-mirror">Dynamic Mirror</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume there are nice web pages on remote hosts we want
to bring into our namespace. For FTP servers we would use
the <code>mirror</code> program which actually maintains an
explicit up-to-date copy of the remote data on the local
machine. For a web server we could use the program
<code>webcopy</code> which runs via HTTP. But both
techniques have a major drawback: The local copy is
always only as up-to-date as the last time we ran the program. It
would be much better if the mirror was not a static one we
have to establish explicitly. Instead we want a dynamic
mirror with data which gets updated automatically
as needed on the remote host(s).</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>To provide this feature we map the remote web page or even
the complete remote web area to our namespace by the use
of the <dfn>Proxy Throughput</dfn> feature
(flag <code>[P]</code>):</p>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^<strong>hotsheet/</strong>(.*)$ <strong>http://www.tstimpreso.com/hotsheet/</strong>$1 [<strong>P</strong>]
</pre></div>
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^<strong>usa-news\.html</strong>$ <strong>http://www.quux-corp.com/news/index.html</strong> [<strong>P</strong>]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="retrieve-missing-data" id="retrieve-missing-data">Retrieve Missing Data from Intranet</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>This is a tricky way of virtually running a corporate
(external) Internet web server
and maintaining its data on an (internal) Intranet web server
firewall. The trick is that the external web server retrieves
the requested data on-the-fly from the internal
one.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>First, we must make sure that our firewall still
protects the internal web server and only the
external web server is allowed to retrieve data from it.
On a packet-filtering firewall, for instance, we could
configure a firewall ruleset like the following:</p>
<div class="example"><pre>
<strong>ALLOW</strong> Host www.quux-corp.dom Port >1024 --> Host www2.quux-corp.dom Port <strong>80</strong>
</pre></div>
<p>Just adjust it to your actual configuration syntax.
Now we can establish the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
rules which request the missing data in the background
through the proxy throughput feature:</p>
<div class="example"><pre>
# REQUEST_FILENAME usage below is correct in this per-server context example
# because the rule that references REQUEST_FILENAME is chained to a rule that
# sets REQUEST_FILENAME.
RewriteCond %{REQUEST_FILENAME} <strong>!-f</strong>
RewriteCond %{REQUEST_FILENAME} <strong>!-d</strong>
RewriteRule ^/home/([^/]+)/.www/?(.*) http://<strong>www2</strong>.quux-corp.dom/~$1/pub/$2 [<strong>P</strong>]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="new-mime-type" id="new-mime-type">New MIME-type, New Service</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>On the net there are many nifty CGI programs. But
their usage is usually boring, so a lot of webmasters
don't use them. Even Apache's Action handler feature for
MIME-types is only appropriate when the CGI programs
don't need special URLs (actually <code>PATH_INFO</code>
and <code>QUERY_STRINGS</code>) as their input. First,
let us configure a new file type with extension
<code>.scgi</code> (for secure CGI) which will be processed
by the popular <code>cgiwrap</code> program. The problem
here is that for instance if we use a Homogeneous URL Layout
(see above) a file inside the user homedirs might have a URL
<code>cgiwrap</code> needs URLs in the form
solves the problem:</p>
<div class="example"><pre>
RewriteRule ^/[uge]/<strong>([^/]+)</strong>/\.www/(.+)\.scgi(.*) ...
... /internal/cgi/user/cgiwrap/~<strong>$1</strong>/$2.scgi$3 [NS,<strong>T=application/x-http-cgi</strong>]
</pre></div>
<p>Or assume we have some more nifty programs:
<code>wwwlog</code> (which displays the
<code>wwwidx</code> (which runs Glimpse on a URL
subtree). We have to provide the URL area to these
programs so they know which area they are really working with.
But usually this is complicated, because they may still be
requested by the alternate URL form, i.e., typically we would
run the <code>swwidx</code> program from within
<div class="example"><pre>
</pre></div>
<p>which is ugly, because we have to hard-code
<strong>both</strong> the location of the area
<strong>and</strong> the location of the CGI inside the
hyperlink. When we have to reorganize, we spend a
lot of time changing the various hyperlinks.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>The solution here is to provide a special new URL format
which automatically leads to the proper CGI invocation.
We configure the following:</p>
<div class="example"><pre>
</pre></div>
<p>Now the hyperlink to search at
<div class="example"><pre>
HREF="*"
</pre></div>
<p>which internally gets automatically transformed to</p>
<div class="example"><pre>
</pre></div>
<p>The same approach leads to an invocation for the
access log CGI program when the hyperlink
<code>:log</code> gets used.</p>
</dd>
</dl>
<div class="section">
<h2><a name="mass-virtual-hosting" id="mass-virtual-hosting">Mass Virtual Hosting</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>The <code class="directive"><a href="/mod/core.html#virtualhost"><VirtualHost></a></code> feature of Apache is nice
and works great when you just have a few dozen
virtual hosts. But when you are an ISP and have hundreds of
virtual hosts, this feature is suboptimal.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>To provide this feature we map the remote web page or even
the complete remote web area to our namespace using the
<dfn>Proxy Throughput</dfn> feature (flag <code>[P]</code>):</p>
<div class="example"><pre>
##
## vhost.map
##
:
</pre></div>
<div class="example"><pre>
##
## httpd.conf
##
:
# use the canonical hostname on redirects, etc.
UseCanonicalName on
:
# add the virtual host in front of the CLF-format
:
# enable the rewriting engine in the main server
RewriteEngine on
# define two maps: one for fixing the URL and one which defines
# the available virtual hosts with their corresponding
# DocumentRoot.
RewriteMap lowercase int:tolower
# Now do the actual virtual host mapping
# via a huge and complicated single rule:
#
# 1. make sure we don't map for common locations
RewriteCond %{REQUEST_URI} !^/commonurl1/.*
RewriteCond %{REQUEST_URI} !^/commonurl2/.*
:
RewriteCond %{REQUEST_URI} !^/commonurlN/.*
#
# 2. make sure we have a Host header, because
# currently our approach only supports
# virtual hosting through this header
RewriteCond %{HTTP_HOST} !^$
#
# 3. lowercase the hostname
RewriteCond ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$
#
# 4. lookup this hostname in vhost.map and
# remember it only when it is a path
# (and not "NONE" from above)
RewriteCond ${vhost:%1} ^(/.*)$
#
# 5. finally we can map the URL to its docroot location
# and remember the virtual host for logging purposes
RewriteRule ^/(.*)$ %1/$1 [E=VHOST:${lowercase:%{HTTP_HOST}}]
:
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="proxy-deny" id="proxy-deny">Proxy Deny</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we forbid a certain host or even a user of a
special host from using the Apache proxy?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We first have to make sure <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
is below(!) <code class="module"><a href="/mod/mod_proxy.html">mod_proxy</a></code> in the Configuration
file when compiling the Apache web server. This way it gets
called <em>before</em> <code class="module"><a href="/mod/mod_proxy.html">mod_proxy</a></code>. Then we
configure the following for a host-dependent deny...</p>
<div class="example"><pre>
RewriteCond %{REMOTE_HOST} <strong>^badhost\.mydomain\.com$</strong>
RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
</pre></div>
<div class="example"><pre>
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} <strong>^badguy@badhost\.mydomain\.com$</strong>
RewriteRule !^http://[^/.]\.mydomain.com.* - [F]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="referer-deflector" id="referer-deflector">Referer-based Deflector</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we program a flexible URL Deflector which acts
on the "Referer" HTTP header and can be configured with as
many referring pages as we like?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Use the following really tricky ruleset...</p>
<div class="example"><pre>
RewriteCond %{HTTP_REFERER} !=""
RewriteCond ${deflector:%{HTTP_REFERER}} ^-$
RewriteRule ^.* %{HTTP_REFERER} [R,L]
RewriteCond %{HTTP_REFERER} !=""
RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^.* ${deflector:%{HTTP_REFERER}} [R,L]
</pre></div>
<p>... in conjunction with a corresponding rewrite
map:</p>
<div class="example"><pre>
##
##
</pre></div>
<p>This automatically redirects the request back to the
referring page (when "<code>-</code>" is used as the value
in the map) or to a specific URL (when an URL is specified
in the map as the second argument).</p>
</dd>
</dl>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/rewrite/rewrite_guide.html" title="English"> en </a> |
<a href="/fr/rewrite/rewrite_guide.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p>
</div><div id="footer">
<p class="apache">Copyright 2009 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>
<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>
</body></html>