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="rewritemap.html">Using RewriteMap</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"><p><code>
75 RewriteEngine on<br />
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"><p><code>
106 RewriteEngine on<br />
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><p><code>
148 RewriteEngine on<br />
149 RewriteRule ^/docs/(.+) http://new.example.com/docs/$1 [R,L]
152 <div class="example"><h3>With RedirectMatch</h3><p><code>
153 RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
156 <div class="example"><h3>With Redirect</h3><p><code>
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"><p><code>
189 RewriteEngine on<br />
190 RewriteBase /~quux/<br />
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"><p><code>
220 # backward compatibility ruleset for<br />
221 # rewriting document.html to document.php<br />
222 # when and only when document.php exists<br />
223 <Directory /var/www/htdocs><br />
224 <span class="indent">
225 RewriteEngine on<br />
226 RewriteBase /var/www/htdocs<br />
228 RewriteCond $1.php -f<br />
229 RewriteCond $1.html !-f<br />
230 RewriteRule ^(.*).html$ $1.php<br />
238 <p>This example uses an often-overlooked feature of mod_rewrite,
239 by taking advantage of the order of execution of the ruleset. In
240 particular, mod_rewrite evaluates the left-hand-side of the
241 RewriteRule before it evaluates the RewriteCond directives.
242 Consequently, $1 is already defined by the time the RewriteCond
243 directives are evaluated. This allows us to test for the existence
244 of the original (<code>document.html</code>) and target
245 (<code>document.php</code>) files using the same base filename.</p>
247 <p>This ruleset is designed to use in a per-directory context (In a
248 <Directory> block or in a .htaccess file), so that the
249 <code>-f</code> checks are looking at the correct directory path.
250 You may need to set a <code class="directive"><a href="../mod/mod_rewite.html#rewritebase">RewriteBase</a></code> directive to specify the
251 directory base that you're working in.</p>
255 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
256 <div class="section">
257 <h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
262 <dt>Description:</dt>
264 <dd>The goal of this rule is to force the use of a particular
265 hostname, in preference to other hostnames which may be used to
266 reach the same site. For example, if you wish to force the use
267 of <strong>www.example.com</strong> instead of
268 <strong>example.com</strong>, you might use a variant of the
269 following recipe.</dd>
275 <p>The very best way to solve this doesn't involve mod_rewrite at all,
276 but rather uses the <code class="directive"><a href="../mod/alias.html#redirect">Redirect</a></code>
277 directive placed in a virtual host for the non-canonical
280 <div class="example"><p><code>
281 <VirtualHost *:80><br />
282 <span class="indent">
283 ServerName undesired.example.com<br />
284 ServerAlias example.com notthis.example.com<br />
286 Redirect / http://www.example.com/<br />
288 </VirtualHost><br />
290 <VirtualHost *:80><br />
291 <span class="indent">
292 ServerName www.example.com<br />
297 <p>You can alternatively accomplish this using the
298 <code class="directive"><a href="../mod/core.html#if"><If></a></code>
301 <div class="example"><p><code>
302 <If "%{HTTP_HOST} != 'www.example.com'"><br />
303 <span class="indent">
304 Redirect / http://www.example.com/
309 <p>Or, for example, to redirect a portion of your site to HTTPS, you
310 might do the following:</p>
312 <div class="example"><p><code>
313 <If "%{SERVER_PROTOCOL} != 'HTTPS'"><br />
314 <span class="indent">
315 Redirect /admin/ https://www.example.com/admin/
320 <p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
321 - if, for example, you need this to work with a larger set of RewriteRules -
322 you might use one of the recipes below.</p>
324 <p>For sites running on a port other than 80:</p>
325 <div class="example"><p><code>
326 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
327 RewriteCond %{HTTP_HOST} !^$<br />
328 RewriteCond %{SERVER_PORT} !^80$<br />
329 RewriteRule ^/?(.*) http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
332 <p>And for a site running on port 80</p>
333 <div class="example"><p><code>
334 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]<br />
335 RewriteCond %{HTTP_HOST} !^$<br />
336 RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
340 If you wanted to do this generically for all domain names - that
341 is, if you want to redirect <strong>example.com</strong> to
342 <strong>www.example.com</strong> for all possible values of
343 <strong>example.com</strong>, you could use the following
346 <div class="example"><p><code>
347 RewriteCond %{HTTP_HOST} !^www\. [NC]<br />
348 RewriteCond %{HTTP_HOST} !^$<br />
349 RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
352 <p>These rulesets will work either in your main server configuration
353 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>
357 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
358 <div class="section">
359 <h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
364 <dt>Description:</dt>
367 <p>A particular resource might exist in one of several places, and
368 we want to look in those places for the resource when it is
369 requested. Perhaps we've recently rearranged our directory
370 structure, dividing content into several locations.</p>
376 <p>The following ruleset searches in two directories to find the
377 resource, and, if not finding it in either place, will attempt to
378 just serve it out of the location requested.</p>
380 <div class="example"><p><code>
381 RewriteEngine on<br />
383 # first try to find it in dir1/...<br />
384 # ...and if found stop and be happy:<br />
385 RewriteCond %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI} -f<br />
386 RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1 [L]<br />
388 # second try to find it in dir2/...<br />
389 # ...and if found stop and be happy:<br />
390 RewriteCond %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI} -f<br />
391 RewriteRule ^(.+) %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1 [L]<br />
393 # else go on for other Alias or ScriptAlias directives,<br />
400 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
401 <div class="section">
402 <h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>
407 <dt>Description:</dt>
410 <p>We have numerous mirrors of our website, and want to redirect
411 people to the one that is located in the country where they are
418 <p>Looking at the hostname of the requesting client, we determine
419 which country they are coming from. If we can't do a lookup on their
420 IP address, we fall back to a default server.</p>
421 <p>We'll use a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
422 directive to build a list of servers that we wish to use.</p>
424 <div class="example"><p><code>
425 HostnameLookups on<br />
426 RewriteEngine on<br />
427 RewriteMap multiplex txt:/path/to/map.mirrors<br />
428 RewriteCond %{REMOTE_HOST} ([a-z]+)$ [NC]<br />
429 RewriteRule ^/(.*)$ ${multiplex:<strong>%1</strong>|http://www.example.com/}$1 [R,L]
432 <div class="example"><p><code>
433 ## map.mirrors -- Multiplexing Map<br />
435 de http://www.example.de/<br />
436 uk http://www.example.uk/<br />
437 com http://www.example.com/<br />
444 <div class="warning">This ruleset relies on
445 <code class="directive"><a href="../mod/core.html#hostnamelookups">HostNameLookups</a></code>
446 being set <code>on</code>, which can be
447 a significant performance hit.</div>
449 <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
450 directive captures the last portion of the hostname of the
451 requesting client - the country code - and the following RewriteRule
452 uses that value to look up the appropriate mirror host in the map
457 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
458 <div class="section">
459 <h2><a name="browser-dependent-content" id="browser-dependent-content">Browser Dependent Content</a></h2>
464 <dt>Description:</dt>
467 <p>We wish to provide different content based on the browser, or
468 user-agent, which is requesting the content.</p>
474 <p>We have to decide, based on the HTTP header "User-Agent",
475 which content to serve. The following config
476 does the following: If the HTTP header "User-Agent"
477 contains "Mozilla/3", the page <code>foo.html</code>
478 is rewritten to <code>foo.NS.html</code> and the
479 rewriting stops. If the browser is "Lynx" or "Mozilla" of
480 version 1 or 2, the URL becomes <code>foo.20.html</code>.
481 All other browsers receive page <code>foo.32.html</code>.
482 This is done with the following ruleset:</p>
484 <div class="example"><p><code>
485 RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/3</strong>.*<br />
486 RewriteRule ^foo\.html$ foo.<strong>NS</strong>.html [<strong>L</strong>]<br />
488 RewriteCond %{HTTP_USER_AGENT} ^<strong>Lynx/</strong>.* [OR]<br />
489 RewriteCond %{HTTP_USER_AGENT} ^<strong>Mozilla/[12]</strong>.*<br />
490 RewriteRule ^foo\.html$ foo.<strong>20</strong>.html [<strong>L</strong>]<br />
492 RewriteRule ^foo\.html$ foo.<strong>32</strong>.html [<strong>L</strong>]
497 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
498 <div class="section">
499 <h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
504 <dt>Description:</dt>
507 <p>On some webservers there is more than one URL for a
508 resource. Usually there are canonical URLs (which are be
509 actually used and distributed) and those which are just
510 shortcuts, internal ones, and so on. Independent of which URL the
511 user supplied with the request, they should finally see the
512 canonical one in their browser address bar.</p>
518 <p>We do an external HTTP redirect for all non-canonical
519 URLs to fix them in the location view of the Browser and
520 for all subsequent requests. In the example ruleset below
521 we replace <code>/puppies</code> and <code>/canines</code>
522 by the canonical <code>/dogs</code>.</p>
524 <div class="example"><p><code>
525 RewriteRule ^/(puppies|canines)/(.*) /dogs/$2 [R]
531 This should really be accomplished with Redirect or RedirectMatch
534 <div class="example"><p><code>
535 RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
540 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
541 <div class="section">
542 <h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
547 <dt>Description:</dt>
550 <p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
551 of the webserver directly relates to the URL "<code>/</code>".
552 But often this data is not really of top-level priority. For example,
553 you may wish for visitors, on first entering a site, to go to a
554 particular subdirectory <code>/about/</code>. This may be accomplished
555 using the following ruleset:</p>
561 <p>We redirect the URL <code>/</code> to
562 <code>/about/</code>:
565 <div class="example"><p><code>
566 RewriteEngine on<br />
567 RewriteRule <strong>^/$</strong> /about/ [<strong>R</strong>]
570 <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>
572 <div class="example"><p><code>
573 RedirectMatch ^/$ http://example.com/about/
576 <p>Note also that the example rewrites only the root URL. That is, it
577 rewrites a request for <code>http://example.com/</code>, but not a
578 request for <code>http://example.com/page.html</code>. If you have in
579 fact changed your document root - that is, if <strong>all</strong> of
580 your content is in fact in that subdirectory, it is greatly preferable
581 to simply change your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
582 directive, or move all of the content up one directory,
583 rather than rewriting URLs.</p>
587 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
588 <div class="section">
589 <h2><a name="fallback-resource" id="fallback-resource">Fallback Resource</a></h2>
593 <dt>Description:</dt>
594 <dd>You want a single resource (say, a certain file, like index.php) to
595 handle all requests that come to a particular directory, except those
596 that should go to an existing resource such as an image, or a css file.</dd>
600 <p>As of version 2.2.16, you should use the <code class="directive"><a href="../mod/mod_dir.html#fallbackresource">FallbackResource</a></code> directive for this:</p>
602 <div class="example"><p><code>
603 <Directory /var/www/my_blog><br />
604 <span class="indent">
605 FallbackResource index.php<br />
610 <p>However, in earlier versions of Apache, or if your needs are more
611 complicated than this, you can use a variation of the following rewrite
612 set to accomplish the same thing:</p>
614 <div class="example"><p><code>
615 <Directory /var/www/my_blog><br />
616 <span class="indent">
617 RewriteBase /my_blog<br />
619 RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f<br />
620 RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d<br />
621 RewriteRule ^ index.php [PT]<br />
626 <p>If, on the other hand, you wish to pass the requested URI as a query
627 string argument to index.php, you can replace that RewriteRule with:</p>
629 <div class="example"><p><code>
630 RewriteRule (.*) index.php?$1 [PT,QSA]
633 <p>Note that these rulesets can be uses in a <code>.htaccess</code>
634 file, as well as in a <Directory> block.</p>
641 <div class="bottomlang">
642 <p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English"> en </a></p>
643 </div><div id="footer">
644 <p class="apache">Copyright 2011 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>
645 <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>