remapping.html.en revision 856d8f134df3b2d23a76717121e042877f85ae3b
<?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>Redirecting and Remapping with mod_rewrite - 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>Redirecting and Remapping with mod_rewrite</h1>
<div class="toplang">
<p><span>Available Languages: </span><a href="/en/rewrite/remapping.html" title="English"> en </a></p>
</div>
<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
how you can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to redirect and remap
request. This includes many examples of common uses of mod_rewrite,
including detailed descriptions of how each works.</p>
<div class="warning">Note that many of these examples won't work unchanged in your
particular server configuration, so it's important that you understand
them, rather than merely cutting and pasting the examples into your
configuration.</div>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#old-to-new">From Old to New (internal)</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#old-to-new-extern">Rewriting From Old to New (external)</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#movehomedirs">Resource Moved to Another Server</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for file extension change</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#browser-dependent-content">Browser Dependent Content</a></li>
</ul><h3>See also</h3><ul class="seealso"><li><a href="/mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li></ul></div>
<div class="section">
<h2><a name="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume we have recently renamed the page
to provide the old URL for backward compatibility. However,
we want that users of the old URL even not recognize that
the pages was renamed - that is, we don't want the address to
change in their browser.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We rewrite the old URL to the new one internally via the
following rule:</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="old-to-new-extern" id="old-to-new-extern">Rewriting From Old to New (external)</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>Assume again that we have recently renamed the page
to provide the old URL for backward compatibility. But this
time we want that the users of the old URL get hinted to
the new one, i.e. their browsers Location field should
change, too.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We force a HTTP redirect to the new URL which leads to a
change of the browsers and thus the users view:</p>
<div class="example"><pre>
RewriteEngine on
RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
</pre></div>
</dd>
<dt>Discussion</dt>
<dd>
<p>In this example, as contrasted to the <a href="#old-to-new-intern">internal</a> example above, we can simply
use the Redirect directive. mod_rewrite was used in that earlier
example in order to hide the redirect from the client:</p>
<div class="example"><p><code>
</code></p></div>
</dd>
</dl>
<div class="section">
<h2><a name="movehomedirs" id="movehomedirs">Resource Moved to Another Server</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>If a resource has moved to another server, you may wish to have
URLs continue to work for a time on the old server while people
update their bookmarks.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>You can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to redirect these URLs
to the new server, but you might also consider using the Redirect
or RedirectMatch directive.</p>
<div class="example"><h3>With mod_rewrite</h3><pre>
RewriteEngine on
RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
</pre></div>
<div class="example"><h3>With RedirectMatch</h3><pre>
RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
</pre></div>
<div class="example"><h3>With Redirect</h3><pre>
Redirect /docs/ http://new.example.com/docs/
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we transform a static page
</dd>
<dt>Solution:</dt>
<dd>
<p>We just rewrite the URL to the CGI-script and force the
handler to be <strong>cgi-script</strong> so that it is
executed as a CGI program.
internally leads to the invocation of
<div class="example"><pre>
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for file extension change</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>How can we make URLs backward compatible (still
bunch of <code>.html</code> files to <code>.php</code>?</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We rewrite the name to its basename and test for
existence of the new extension. If it exists, we take
that name, else we rewrite the URL to its original state.</p>
<div class="example"><pre>
# backward compatibility ruleset for
# rewriting document.html to document.php
# when and only when document.php exists
RewriteEngine on
RewriteCond $1.php -f
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1.php
</Directory>
</pre></div>
</dd>
<dt>Discussion</dt>
<dd>
<p>This example uses an often-overlooked feature of mod_rewrite,
by taking advantage of the order of execution of the ruleset. In
particular, mod_rewrite evaluates the left-hand-side of the
RewriteRule before it evaluates the RewriteCond directives.
Consequently, $1 is already defined by the time the RewriteRule
directives are evaluated. This allows us to test for the existence
<p>This ruleset is designed to use in a per-directory context (In a
<Directory> block or in a .htaccess file), so that the
<code>-f</code> checks are looking at the correct directory path.
You may need to set a <code class="directive"><a href="/mod/mod_rewite.html#rewritebase">RewriteBase</a></code> directive to specify the
directory base that you're working in.</p>
</dd>
</dl>
<div class="section">
<h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
<dl>
<dt>Description:</dt>
<dd>The goal of this rule is to force the use of a particular
hostname, in preference to other hostnames which may be used to
reach the same site. For example, if you wish to force the use
following recipe.</dd>
<dt>Solution:</dt>
<dd>
<p>For sites running on a port other than 80:</p>
<div class="example"><pre>
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
</pre></div>
<p>And for a site running on port 80</p>
<div class="example"><pre>
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
</pre></div>
<p>
If you wanted to do this generically for all domain names - that
recipe:</p>
<div class="example"><pre>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
</pre></div>
<p>These rulesets will work either in your main server configuration
file, or in a <code>.htaccess</code> file placed in the <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code> of the server.</p>
</dd>
</dl>
<div class="section">
<h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>A particular resource might exist in one of several places, and
we want to look in those places for the resource when it is
requested. Perhaps we've recently rearranged our directory
structure, dividing content into several locations.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>The following ruleset searches in two directories to find the
resource, and, if not finding it in either place, will attempt to
just serve it out of the location requested.</p>
<div class="example"><pre>
RewriteEngine on
# first try to find it in dir1/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]
# second try to find it in dir2/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]
# else go on for other Alias or ScriptAlias directives,
# etc.
RewriteRule ^(.+) - [PT]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We have numerous mirrors of our website, and want to redirect
people to the one that is located in the country where they are
located.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>Looking at the hostname of the requesting client, we determine
which country they are coming from. If we can't do a lookup on their
IP address, we fall back to a default server.</p>
<p>We'll use <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
directive to build a list of servers that we wish to use.</p>
<div class="example"><pre>
HostnameLookups on
RewriteEngine on
RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
</pre></div>
<div class="example"><pre>
## map.mirrors -- Multiplexing Map
##EOF##
</pre></div>
</dd>
<dt>Discussion</dt>
<dd>
<div class="warning">This ruleset relies on
being set <code>on</code>, which can be
a significant performance hit.</div>
directive captures the last portion of the hostname of the
requesting client - the country code - and the following RewriteRule
uses that value to look up the appropriate mirror host in the map
file.</p>
</dd>
</dl>
<div class="section">
<h2><a name="browser-dependent-content" id="browser-dependent-content">Browser Dependent Content</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We wish to provide different content based on the browser, or
user-agent, which is requesting the content.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We have to decide, based on the HTTP header "User-Agent",
which content to serve. The following config
does the following: If the HTTP header "User-Agent"
rewriting stops. If the browser is "Lynx" or "Mozilla" of
This is done with the following ruleset:</p>
<div class="example"><pre>
RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*
RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]
RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong>.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong>.*
RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]
RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
</pre></div>
</dd>
</dl>
<div class="section">
<h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>On some webservers there is more than one URL for a
resource. Usually there are canonical URLs (which are be
actually used and distributed) and those which are just
shortcuts, internal ones, and so on. Independent of which URL the
user supplied with the request, they should finally see the
canonical one in their browser address bar.</p>
</dd>
<dt>Solution:</dt>
<dd>
<p>We do an external HTTP redirect for all non-canonical
URLs to fix them in the location view of the Browser and
for all subsequent requests. In the example ruleset below
we replace <code>/puppies</code> and <code>/canines</code>
by the canonical <code>/dogs</code>.</p>
<div class="example"><pre>
RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
</pre></div>
</dd>
<dt>Discussion:</dt>
<dd>
This should really be accomplished with Redirect or RedirectMatch
directives:
<div class="example"><pre>
RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
</pre></div>
</dd>
</dl>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/rewrite/remapping.html" title="English"> en </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>