1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5 This file is generated from xml source: DO NOT EDIT
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8 <title>Redirecting and Remapping with mod_rewrite - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <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>
15 <p class="apache">Apache HTTP Server Version 2.3</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
19 <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>
21 <p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English"> en </a></p>
25 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
26 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
27 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect and remap
28 request. This includes many examples of common uses of mod_rewrite,
29 including detailed descriptions of how each works.</p>
31 <div class="warning">Note that many of these examples won't work unchanged in your
32 particular server configuration, so it's important that you understand
33 them, rather than merely cutting and pasting the examples into your
37 <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>
38 <li><img alt="" src="../images/down.gif" /> <a href="#old-to-new-extern">Rewriting From Old to New (external)</a></li>
39 <li><img alt="" src="../images/down.gif" /> <a href="#movehomedirs">Resource Moved to Another Server</a></li>
40 <li><img alt="" src="../images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
41 <li><img alt="" src="../images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for file extension change</a></li>
42 <li><img alt="" src="../images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
43 <li><img alt="" src="../images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
44 <li><img alt="" src="../images/down.gif" /> <a href="#archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></li>
45 <li><img alt="" src="../images/down.gif" /> <a href="#browser-dependent-content">Browser Dependent Content</a></li>
46 <li><img alt="" src="../images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
47 <li><img alt="" src="../images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
48 <li><img alt="" src="../images/down.gif" /> <a href="#fallback-resource">Fallback Resource</a></li>
49 </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><li><a href="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="advanced.html">Advanced techniques and tricks</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul></div>
50 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
52 <h2><a name="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>
60 <p>Assume we have recently renamed the page
61 <code>foo.html</code> to <code>bar.html</code> and now want
62 to provide the old URL for backward compatibility. However,
63 we want that users of the old URL even not recognize that
64 the pages was renamed - that is, we don't want the address to
65 change in their browser.</p>
71 <p>We rewrite the old URL to the new one internally via the
74 <div class="example"><pre>
76 RewriteRule ^<strong>/old</strong>\.html$ <strong>/new</strong>.html [PT]
81 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
83 <h2><a name="old-to-new-extern" id="old-to-new-extern">Rewriting From Old to New (external)</a></h2>
91 <p>Assume again that we have recently renamed the page
92 <code>foo.html</code> to <code>bar.html</code> and now want
93 to provide the old URL for backward compatibility. But this
94 time we want that the users of the old URL get hinted to
95 the new one, i.e. their browsers Location field should
102 <p>We force a HTTP redirect to the new URL which leads to a
103 change of the browsers and thus the users view:</p>
105 <div class="example"><pre>
107 RewriteRule ^<strong>/foo</strong>\.html$ <strong>bar</strong>.html [<strong>R</strong>]
114 <p>In this example, as contrasted to the <a href="#old-to-new-intern">internal</a> example above, we can simply
115 use the Redirect directive. mod_rewrite was used in that earlier
116 example in order to hide the redirect from the client:</p>
118 <div class="example"><p><code>
119 Redirect /foo.html /bar.html
125 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
126 <div class="section">
127 <h2><a name="movehomedirs" id="movehomedirs">Resource Moved to Another Server</a></h2>
132 <dt>Description:</dt>
135 <p>If a resource has moved to another server, you may wish to have
136 URLs continue to work for a time on the old server while people
137 update their bookmarks.</p>
143 <p>You can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect these URLs
144 to the new server, but you might also consider using the Redirect
145 or RedirectMatch directive.</p>
147 <div class="example"><h3>With mod_rewrite</h3><pre>
149 RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
152 <div class="example"><h3>With RedirectMatch</h3><pre>
153 RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
156 <div class="example"><h3>With Redirect</h3><pre>
157 Redirect /docs/ http://new.example.com/docs/
162 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
163 <div class="section">
164 <h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>
169 <dt>Description:</dt>
172 <p>How can we transform a static page
173 <code>foo.html</code> into a dynamic variant
174 <code>foo.cgi</code> in a seamless way, i.e. without notice
175 by the browser/user.</p>
181 <p>We just rewrite the URL to the CGI-script and force the
182 handler to be <strong>cgi-script</strong> so that it is
183 executed as a CGI program.
184 This way a request to <code>/~quux/foo.html</code>
185 internally leads to the invocation of
186 <code>/~quux/foo.cgi</code>.</p>
188 <div class="example"><pre>
191 RewriteRule ^foo\.<strong>html</strong>$ foo.<strong>cgi</strong> [H=<strong>cgi-script</strong>]
196 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
197 <div class="section">
198 <h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for file extension change</a></h2>
203 <dt>Description:</dt>
206 <p>How can we make URLs backward compatible (still
207 existing virtually) after migrating <code>document.YYYY</code>
208 to <code>document.XXXX</code>, e.g. after translating a
209 bunch of <code>.html</code> files to <code>.php</code>?</p>
215 <p>We rewrite the name to its basename and test for
216 existence of the new extension. If it exists, we take
217 that name, else we rewrite the URL to its original state.</p>
219 <div class="example"><pre>
220 # backward compatibility ruleset for
221 # rewriting document.html to document.php
222 # when and only when document.php exists
223 <Directory /var/www/htdocs>
225 RewriteBase /var/www/htdocs
227 RewriteCond $1.php -f
228 RewriteCond $1.html !-f
229 RewriteRule ^(.*).html$ $1.php
236 <p>This example uses an often-overlooked feature of mod_rewrite,
237 by taking advantage of the order of execution of the ruleset. In
238 particular, mod_rewrite evaluates the left-hand-side of the
239 RewriteRule before it evaluates the RewriteCond directives.
240 Consequently, $1 is already defined by the time the RewriteCond
241 directives are evaluated. This allows us to test for the existence
242 of the original (<code>document.html</code>) and target
243 (<code>document.php</code>) files using the same base filename.</p>
245 <p>This ruleset is designed to use in a per-directory context (In a
246 <Directory> block or in a .htaccess file), so that the
247 <code>-f</code> checks are looking at the correct directory path.
248 You may need to set a <code class="directive"><a href="../mod/mod_rewite.html#rewritebase">RewriteBase</a></code> directive to specify the
249 directory base that you're working in.</p>
253 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
254 <div class="section">
255 <h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
260 <dt>Description:</dt>
262 <dd>The goal of this rule is to force the use of a particular
263 hostname, in preference to other hostnames which may be used to
264 reach the same site. For example, if you wish to force the use
265 of <strong>www.example.com</strong> instead of
266 <strong>example.com</strong>, you might use a variant of the
267 following recipe.</dd>
273 <p>The very best way to solve this doesn't involve mod_rewrite at all,
274 but rather uses the <code class="directive"><a href="../mod/alias.html#redirect">Redirect</a></code>
275 directive places in a virtual host for the non-canonical
278 <div class="example"><pre>
279 <VirtualHost *:80>
280 ServerName undesired.example.com
281 ServerAlias example.com notthis.example.com
283 Redirect / http://www.example.com/
287 <p>However, there are situations where you'll need to use mod_rewrite -
288 primarily when you don't have access to the main server configuration
289 file, or if you wish to do this dynamically for a larger number of
290 hostnames. For these situations, you might use one of the recipes
293 <p>For sites running on a port other than 80:</p>
294 <div class="example"><pre>
295 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
296 RewriteCond %{HTTP_HOST} !^$
297 RewriteCond %{SERVER_PORT} !^80$
298 RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
301 <p>And for a site running on port 80</p>
302 <div class="example"><pre>
303 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
304 RewriteCond %{HTTP_HOST} !^$
305 RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
309 If you wanted to do this generically for all domain names - that
310 is, if you want to redirect <strong>example.com</strong> to
311 <strong>www.example.com</strong> for all possible values of
312 <strong>example.com</strong>, you could use the following
315 <div class="example"><pre>
316 RewriteCond %{HTTP_HOST} !^www\. [NC]
317 RewriteCond %{HTTP_HOST} !^$
318 RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
321 <p>These rulesets will work either in your main server configuration
322 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>
326 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
327 <div class="section">
328 <h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
333 <dt>Description:</dt>
336 <p>A particular resource might exist in one of several places, and
337 we want to look in those places for the resource when it is
338 requested. Perhaps we've recently rearranged our directory
339 structure, dividing content into several locations.</p>
345 <p>The following ruleset searches in two directories to find the
346 resource, and, if not finding it in either place, will attempt to
347 just serve it out of the location requested.</p>
349 <div class="example"><pre>
352 # first try to find it in dir1/...
353 # ...and if found stop and be happy:
354 RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f
355 RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]
357 # second try to find it in dir2/...
358 # ...and if found stop and be happy:
359 RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f
360 RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]
362 # else go on for other Alias or ScriptAlias directives,
369 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
370 <div class="section">
371 <h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>
376 <dt>Description:</dt>
379 <p>We have numerous mirrors of our website, and want to redirect
380 people to the one that is located in the country where they are
387 <p>Looking at the hostname of the requesting client, we determine
388 which country they are coming from. If we can't do a lookup on their
389 IP address, we fall back to a default server.</p>
390 <p>We'll use a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
391 directive to build a list of servers that we wish to use.</p>
393 <div class="example"><pre>
396 RewriteMap multiplex txt:/path/to/map.mirrors
397 RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]
398 RewriteRule ^/(.*)$ ${multiplex:<strong>%1</strong>|http://www.example.com/}$1 [R,L]
401 <div class="example"><pre>
402 ## map.mirrors -- Multiplexing Map
404 de http://www.example.de/
405 uk http://www.example.uk/
406 com http://www.example.com/
413 <div class="warning">This ruleset relies on
414 <code class="directive"><a href="../mod/core.html#hostnamelookups">HostNameLookups</a></code>
415 being set <code>on</code>, which can be
416 a significant performance hit.</div>
418 <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
419 directive captures the last portion of the hostname of the
420 requesting client - the country code - and the following RewriteRule
421 uses that value to look up the appropriate mirror host in the map
426 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
427 <div class="section">
428 <h2><a name="browser-dependent-content" id="browser-dependent-content">Browser Dependent Content</a></h2>
433 <dt>Description:</dt>
436 <p>We wish to provide different content based on the browser, or
437 user-agent, which is requesting the content.</p>
443 <p>We have to decide, based on the HTTP header "User-Agent",
444 which content to serve. The following config
445 does the following: If the HTTP header "User-Agent"
446 contains "Mozilla/3", the page <code>foo.html</code>
447 is rewritten to <code>foo.NS.html</code> and the
448 rewriting stops. If the browser is "Lynx" or "Mozilla" of
449 version 1 or 2, the URL becomes <code>foo.20.html</code>.
450 All other browsers receive page <code>foo.32.html</code>.
451 This is done with the following ruleset:</p>
453 <div class="example"><pre>
454 RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*
455 RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]
457 RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong>.* [OR]
458 RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong>.*
459 RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]
461 RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
466 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
467 <div class="section">
468 <h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
473 <dt>Description:</dt>
476 <p>On some webservers there is more than one URL for a
477 resource. Usually there are canonical URLs (which are be
478 actually used and distributed) and those which are just
479 shortcuts, internal ones, and so on. Independent of which URL the
480 user supplied with the request, they should finally see the
481 canonical one in their browser address bar.</p>
487 <p>We do an external HTTP redirect for all non-canonical
488 URLs to fix them in the location view of the Browser and
489 for all subsequent requests. In the example ruleset below
490 we replace <code>/puppies</code> and <code>/canines</code>
491 by the canonical <code>/dogs</code>.</p>
493 <div class="example"><pre>
494 RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
500 This should really be accomplished with Redirect or RedirectMatch
503 <div class="example"><pre>
504 RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
509 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
510 <div class="section">
511 <h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
516 <dt>Description:</dt>
519 <p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
520 of the webserver directly relates to the URL "<code>/</code>".
521 But often this data is not really of top-level priority. For example,
522 you may wish for visitors, on first entering a site, to go to a
523 particular subdirectory <code>/about/</code>. This may be accomplished
524 using the following ruleset:</p>
530 <p>We redirect the URL <code>/</code> to
531 <code>/about/</code>:
534 <div class="example"><pre>
536 RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
539 <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>
541 <div class="example"><p><code>
542 RedirectMatch ^/$ http://example.com/about/
545 <p>Note also that the example rewrites only the root URL. That is, it
546 rewrites a request for <code>http://example.com/</code>, but not a
547 request for <code>http://example.com/page.html</code>. If you have in
548 fact changed your document root - that is, if <strong>all</strong> of
549 your content is in fact in that subdirectory, it is greatly preferable
550 to simply change your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
551 directive, or move all of the content up one directory,
552 rather than rewriting URLs.</p>
556 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
557 <div class="section">
558 <h2><a name="fallback-resource" id="fallback-resource">Fallback Resource</a></h2>
562 <dt>Description:</dt>
563 <dd>You want a single resource (say, a certain file, like index.php) to
564 handle all requests that come to a particular directory, except those
565 that should go to an existing resource such as an image, or a css file.</dd>
569 <p>As of version 2.4, you should use the <code class="directive"><a href="../mod/mod_dir.html#fallbackresource">FallbackResource</a></code> directive for this:</p>
571 <div class="example"><pre>
572 <Directory /var/www/my_blog>
573 FallbackResource index.php
577 <p>However, in earlier versions of Apache, or if your needs are more
578 complicated than this, you can use a variation of the following rewrite
579 set to accomplish the same thing:</p>
581 <div class="example"><pre>
582 <Directory /var/www/my_blog>
585 RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f
586 RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d
587 RewriteRule ^ index.php [PT]
591 <p>If, on the other hand, you wish to pass the requested URI as a query
592 string argument to index.php, you can replace that RewriteRule with:</p>
594 <div class="example"><pre>
595 RewriteRule (.*) index.php?$1 [PT,QSA]
598 <p>Note that these rulesets can be uses in a <code>.htaccess</code>
599 file, as well as in a <Directory> block.</p>
606 <div class="bottomlang">
607 <p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English"> en </a></p>
608 </div><div id="footer">
609 <p class="apache">Copyright 2010 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>
610 <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>