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
7      -->
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" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" />
12<script src="/style/scripts/prettify.js" type="text/javascript">
13</script>
14
15<link href="/images/favicon.ico" rel="shortcut icon" /></head>
16<body id="manual-page"><div id="page-header">
17<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
18<p class="apache">Apache HTTP Server Version 2.2</p>
19<img alt="" src="/images/feather.gif" /></div>
20<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
21<div id="path">
22<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.2</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Redirecting and Remapping with mod_rewrite</h1>
23<div class="toplang">
24<p><span>Available Languages: </span><a href="/en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
25</div>
26
27
28<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> 
29<a href="/mod/mod_rewrite.html">reference documentation</a>. It describes
30how you can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to redirect and remap
31request. This includes many examples of common uses of mod_rewrite,
32including detailed descriptions of how each works.</p>
33
34<div class="warning">Note that many of these examples won't work unchanged in your
35particular server configuration, so it's important that you understand
36them, rather than merely cutting and pasting the examples into your
37configuration.</div>
38
39</div>
40<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>
41<li><img alt="" src="/images/down.gif" /> <a href="#old-to-new-extern">Rewriting From Old to New (external)</a></li>
42<li><img alt="" src="/images/down.gif" /> <a href="#movehomedirs">Resource Moved to Another Server</a></li>
43<li><img alt="" src="/images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
44<li><img alt="" src="/images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for file extension change</a></li>
45<li><img alt="" src="/images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
46<li><img alt="" src="/images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
47<li><img alt="" src="/images/down.gif" /> <a href="#archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></li>
48<li><img alt="" src="/images/down.gif" /> <a href="#browser-dependent-content">Browser Dependent Content</a></li>
49<li><img alt="" src="/images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
50<li><img alt="" src="/images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
51<li><img alt="" src="/images/down.gif" /> <a href="#fallback-resource">Fallback Resource</a></li>
52</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><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
53<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
54<div class="section">
55<h2><a name="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>
56
57  
58
59  <dl>
60    <dt>Description:</dt>
61
62    <dd>
63      <p>Assume we have recently renamed the page
64      <code>foo.html</code> to <code>bar.html</code> and now want
65      to provide the old URL for backward compatibility. However,
66      we want that users of the old URL even not recognize that
67      the pages was renamed - that is, we don't want the address to
68      change in their browser.</p>
69    </dd>
70
71    <dt>Solution:</dt>
72
73    <dd>
74      <p>We rewrite the old URL to the new one internally via the
75      following rule:</p>
76
77<div class="example"><p><code>
78RewriteEngine  on<br />
79RewriteRule    ^<strong>/foo</strong>\.html$  <strong>/bar</strong>.html [PT]
80</code></p></div>
81    </dd>
82  </dl>
83
84</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
85<div class="section">
86<h2><a name="old-to-new-extern" id="old-to-new-extern">Rewriting From Old to New (external)</a></h2>
87
88  
89
90  <dl>
91    <dt>Description:</dt>
92
93    <dd>
94      <p>Assume again that we have recently renamed the page
95      <code>foo.html</code> to <code>bar.html</code> and now want
96      to provide the old URL for backward compatibility. But this
97      time we want that the users of the old URL get hinted to
98      the new one, i.e. their browsers Location field should
99      change, too.</p>
100    </dd>
101
102    <dt>Solution:</dt>
103
104    <dd>
105      <p>We force a HTTP redirect to the new URL which leads to a
106      change of the browsers and thus the users view:</p>
107
108<div class="example"><p><code>
109RewriteEngine  on<br />
110RewriteRule    ^<strong>/foo</strong>\.html$  <strong>bar</strong>.html  [<strong>R</strong>]
111</code></p></div>
112</dd>
113
114<dt>Discussion</dt>
115
116    <dd>
117    <p>In this example, as contrasted to the <a href="#old-to-new-intern">internal</a> example above, we can simply
118    use the Redirect directive. mod_rewrite was used in that earlier
119    example in order to hide the redirect from the client:</p>
120
121    <div class="example"><p><code>
122    Redirect /foo.html /bar.html
123    </code></p></div>
124
125    </dd>
126  </dl>
127
128</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
129<div class="section">
130<h2><a name="movehomedirs" id="movehomedirs">Resource Moved to Another Server</a></h2>
131
132  
133
134  <dl>
135    <dt>Description:</dt>
136
137    <dd>
138      <p>If a resource has moved to another server, you may wish to have
139      URLs continue to work for a time on the old server while people
140      update their bookmarks.</p>
141    </dd>
142
143    <dt>Solution:</dt>
144
145    <dd>
146      <p>You can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to redirect these URLs
147      to the new server, but you might also consider using the Redirect
148      or RedirectMatch directive.</p>
149
150<div class="example"><h3>With mod_rewrite</h3><p><code>
151RewriteEngine on<br />
152RewriteRule   ^/docs/(.+)  http://new.example.com/docs/$1  [R,L]
153</code></p></div>
154
155<div class="example"><h3>With RedirectMatch</h3><p><code>
156RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
157</code></p></div>
158
159<div class="example"><h3>With Redirect</h3><p><code>
160Redirect /docs/ http://new.example.com/docs/
161</code></p></div>
162    </dd>
163  </dl>
164
165</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
166<div class="section">
167<h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>
168
169  
170
171  <dl>
172    <dt>Description:</dt>
173
174    <dd>
175      <p>How can we transform a static page
176      <code>foo.html</code> into a dynamic variant
177      <code>foo.cgi</code> in a seamless way, i.e. without notice
178      by the browser/user.</p>
179    </dd>
180
181    <dt>Solution:</dt>
182
183    <dd>
184      <p>We just rewrite the URL to the CGI-script and force the
185      handler to be <strong>cgi-script</strong> so that it is
186      executed as a CGI program.
187      This way a request to <code>/~quux/foo.html</code>
188      internally leads to the invocation of
189      <code>/~quux/foo.cgi</code>.</p>
190
191<div class="example"><p><code>
192RewriteEngine  on<br />
193RewriteBase    /~quux/<br />
194RewriteRule    ^foo\.<strong>html</strong>$  foo.<strong>cgi</strong>  [H=<strong>cgi-script</strong>]
195</code></p></div>
196    </dd>
197  </dl>
198
199</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
200<div class="section">
201<h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for file extension change</a></h2>
202
203  
204
205  <dl>
206    <dt>Description:</dt>
207
208    <dd>
209      <p>How can we make URLs backward compatible (still
210      existing virtually) after migrating <code>document.YYYY</code>
211      to <code>document.XXXX</code>, e.g. after translating a
212      bunch of <code>.html</code> files to <code>.php</code>?</p>
213    </dd>
214
215    <dt>Solution:</dt>
216
217    <dd>
218      <p>We rewrite the name to its basename and test for
219      existence of the new extension. If it exists, we take
220      that name, else we rewrite the URL to its original state.</p>
221
222<div class="example"><p><code>
223#   backward compatibility ruleset for<br />
224#   rewriting document.html to document.php<br />
225#   when and only when document.php exists<br />
226&lt;Directory /var/www/htdocs&gt;<br />
227<span class="indent">
228RewriteEngine on<br />
229RewriteBase /var/www/htdocs<br />
230<br />
231RewriteCond $1.php -f<br />
232RewriteCond $1.html !-f<br />
233RewriteRule ^(.*).html$ $1.php<br />
234</span>
235&lt;/Directory&gt;
236</code></p></div>
237    </dd>
238
239    <dt>Discussion</dt>
240    <dd>
241    <p>This example uses an often-overlooked feature of mod_rewrite,
242    by taking advantage of the order of execution of the ruleset. In
243    particular, mod_rewrite evaluates the left-hand-side of the
244    RewriteRule before it evaluates the RewriteCond directives.
245    Consequently, $1 is already defined by the time the RewriteCond
246    directives are evaluated. This allows us to test for the existence
247    of the original (<code>document.html</code>) and target
248    (<code>document.php</code>) files using the same base filename.</p>
249
250    <p>This ruleset is designed to use in a per-directory context (In a
251    &lt;Directory&gt; block or in a .htaccess file), so that the
252    <code>-f</code> checks are looking at the correct directory path.
253    You may need to set a <code class="directive"><a href="/mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> directive to specify the
254    directory base that you're working in.</p>
255    </dd>
256  </dl>
257
258</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
259<div class="section">
260<h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
261
262
263
264      <dl>
265        <dt>Description:</dt>
266
267        <dd>The goal of this rule is to force the use of a particular
268        hostname, in preference to other hostnames which may be used to
269        reach the same site. For example, if you wish to force the use
270        of <strong>www.example.com</strong> instead of
271        <strong>example.com</strong>, you might use a variant of the
272        following recipe.</dd>
273
274        <dt>Solution:</dt>
275
276        <dd>
277
278<p>The very best way to solve this doesn't involve mod_rewrite at all,
279but rather uses the <code class="directive"><a href="/mod/mod_alias.html#redirect">Redirect</a></code>
280directive placed in a virtual host for the non-canonical
281hostname(s).</p>
282
283<div class="example"><p><code>
284&lt;VirtualHost *:80&gt;<br />
285<span class="indent">
286  ServerName undesired.example.com<br />
287  ServerAlias example.com notthis.example.com<br />
288<br />
289  Redirect / http://www.example.com/<br />
290</span>
291&lt;/VirtualHost&gt;<br />
292<br />
293&lt;VirtualHost *:80&gt;<br />
294<span class="indent">
295  ServerName www.example.com<br />
296</span>
297&lt;/VirtualHost&gt;
298</code></p></div>
299
300<p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
301- if, for example, you need this to work with a larger set of RewriteRules - 
302you might use one of the recipes below.</p>
303
304<p>For sites running on a port other than 80:</p>
305<div class="example"><p><code>
306RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]<br />
307RewriteCond %{HTTP_HOST}   !^$<br />
308RewriteCond %{SERVER_PORT} !^80$<br />
309RewriteRule ^/?(.*)         http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
310</code></p></div>
311
312<p>And for a site running on port 80</p>
313<div class="example"><p><code>
314RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]<br />
315RewriteCond %{HTTP_HOST}   !^$<br />
316RewriteRule ^/?(.*)         http://www.example.com/$1 [L,R,NE]
317</code></p></div>
318
319        <p>
320        If you wanted to do this generically for all domain names - that
321        is, if you want to redirect <strong>example.com</strong> to
322        <strong>www.example.com</strong> for all possible values of
323        <strong>example.com</strong>, you could use the following
324        recipe:</p>
325
326<div class="example"><p><code>
327RewriteCond %{HTTP_HOST} !^www\. [NC]<br />
328RewriteCond %{HTTP_HOST} !^$<br />
329RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
330</code></p></div>
331
332    <p>These rulesets will work either in your main server configuration
333    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>
334        </dd>
335      </dl>
336
337</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
338<div class="section">
339<h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
340
341  
342
343  <dl>
344    <dt>Description:</dt>
345
346    <dd>
347      <p>A particular resource might exist in one of several places, and
348      we want to look in those places for the resource when it is
349      requested. Perhaps we've recently rearranged our directory
350      structure, dividing content into several locations.</p>
351    </dd>
352
353    <dt>Solution:</dt>
354
355    <dd>
356      <p>The following ruleset searches in two directories to find the
357      resource, and, if not finding it in either place, will attempt to
358      just serve it out of the location requested.</p>
359
360<div class="example"><p><code>
361RewriteEngine on<br />
362<br />
363#   first try to find it in dir1/...<br />
364#   ...and if found stop and be happy:<br />
365RewriteCond         %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI}  -f<br />
366RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1  [L]<br />
367<br />
368#   second try to find it in dir2/...<br />
369#   ...and if found stop and be happy:<br />
370RewriteCond         %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI}  -f<br />
371RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1  [L]<br />
372<br />
373#   else go on for other Alias or ScriptAlias directives,<br />
374#   etc.<br />
375RewriteRule   ^  -  [PT]
376</code></p></div>
377    </dd>
378  </dl>
379
380</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
381<div class="section">
382<h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>
383
384  
385
386  <dl>
387    <dt>Description:</dt>
388
389    <dd>
390    <p>We have numerous mirrors of our website, and want to redirect
391    people to the one that is located in the country where they are
392    located.</p>
393    </dd>
394
395    <dt>Solution:</dt>
396
397    <dd>
398    <p>Looking at the hostname of the requesting client, we determine
399    which country they are coming from. If we can't do a lookup on their
400    IP address, we fall back to a default server.</p>
401    <p>We'll use a <code class="directive"><a href="/mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
402    directive to build a list of servers that we wish to use.</p>
403
404<div class="example"><p><code>
405HostnameLookups on<br />
406RewriteEngine on<br />
407RewriteMap    multiplex         txt:/path/to/map.mirrors<br />
408RewriteCond  %{REMOTE_HOST}     ([a-z]+)$ [NC]<br />
409RewriteRule   ^/(.*)$  ${multiplex:<strong>%1</strong>|http://www.example.com/}$1  [R,L]
410</code></p></div>
411
412<div class="example"><p><code>
413##  map.mirrors -- Multiplexing Map<br />
414<br />
415de        http://www.example.de/<br />
416uk        http://www.example.uk/<br />
417com       http://www.example.com/<br />
418##EOF##
419</code></p></div>
420    </dd>
421
422    <dt>Discussion</dt>
423    <dd>
424    <div class="warning">This ruleset relies on 
425    <code class="directive"><a href="/mod/core.html#hostnamelookups">HostNameLookups</a></code> 
426    being set <code>on</code>, which can be
427    a significant performance hit.</div>
428
429    <p>The <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
430    directive captures the last portion of the hostname of the
431    requesting client - the country code - and the following RewriteRule
432    uses that value to look up the appropriate mirror host in the map
433    file.</p>
434    </dd>
435  </dl>
436
437</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
438<div class="section">
439<h2><a name="browser-dependent-content" id="browser-dependent-content">Browser Dependent Content</a></h2>
440
441  
442
443  <dl>
444    <dt>Description:</dt>
445
446    <dd>
447      <p>We wish to provide different content based on the browser, or
448      user-agent, which is requesting the content.</p>
449    </dd>
450
451    
452
453    <dt>Solution:</dt>
454
455    <dd>
456      <p>We have to decide, based on the HTTP header "User-Agent",
457      which content to serve. The following config
458      does the following: If the HTTP header "User-Agent"
459      contains "Mozilla/3", the page <code>foo.html</code>
460      is rewritten to <code>foo.NS.html</code> and the
461      rewriting stops. If the browser is "Lynx" or "Mozilla" of
462      version 1 or 2, the URL becomes <code>foo.20.html</code>.
463      All other browsers receive page <code>foo.32.html</code>.
464      This is done with the following ruleset:</p>
465
466<div class="example"><p><code>
467RewriteCond %{HTTP_USER_AGENT}  <strong>^Mozilla/3</strong>.*<br />
468RewriteRule ^foo\.html$         foo.<strong>NS</strong>.html          [<strong>L</strong>]<br />
469<br />
470RewriteCond %{HTTP_USER_AGENT}  <strong>^Lynx/</strong>         [OR]<br />
471RewriteCond %{HTTP_USER_AGENT}  <strong>^Mozilla/[12]</strong><br />
472RewriteRule ^foo\.html$         foo.<strong>20</strong>.html          [<strong>L</strong>]<br />
473<br />
474RewriteRule ^foo\.html$         foo.<strong>32</strong>.html          [<strong>L</strong>]
475</code></p></div>
476    </dd>
477  </dl>
478
479</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
480<div class="section">
481<h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
482
483
484
485<dl>
486 <dt>Description:</dt>
487
488   <dd>
489     <p>On some webservers there is more than one URL for a
490     resource. Usually there are canonical URLs (which are be
491     actually used and distributed) and those which are just
492     shortcuts, internal ones, and so on. Independent of which URL the
493     user supplied with the request, they should finally see the
494     canonical one in their browser address bar.</p>
495   </dd>
496
497   <dt>Solution:</dt>
498
499     <dd>
500       <p>We do an external HTTP redirect for all non-canonical
501       URLs to fix them in the location view of the Browser and
502       for all subsequent requests. In the example ruleset below
503       we replace <code>/puppies</code> and <code>/canines</code>
504       by the canonical <code>/dogs</code>.</p>
505
506<div class="example"><p><code>
507RewriteRule   ^/(puppies|canines)/(.*)    /dogs/$2  [R]
508</code></p></div>
509        </dd>
510
511     <dt>Discussion:</dt>
512     <dd>
513     This should really be accomplished with Redirect or RedirectMatch
514     directives:
515
516     <div class="example"><p><code>
517     RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
518     </code></p></div>
519     </dd>
520      </dl>
521
522</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
523<div class="section">
524<h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
525
526  
527
528  <dl>
529    <dt>Description:</dt>
530
531    <dd>
532<p>Usually the <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
533of the webserver directly relates to the URL "<code>/</code>".
534But often this data is not really of top-level priority. For example,
535you may wish for visitors, on first entering a site, to go to a
536particular subdirectory <code>/about/</code>. This may be accomplished
537using the following ruleset:</p>
538</dd>
539
540    <dt>Solution:</dt>
541
542    <dd>
543      <p>We redirect the URL <code>/</code> to
544      <code>/about/</code>:
545      </p>
546     
547<div class="example"><p><code>
548RewriteEngine on<br />
549RewriteRule   <strong>^/$</strong>  /about/  [<strong>R</strong>]
550</code></p></div>
551
552<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>
553
554<div class="example"><p><code>
555RedirectMatch ^/$ http://example.com/about/
556</code></p></div>
557
558<p>Note also that the example rewrites only the root URL. That is, it
559rewrites a request for <code>http://example.com/</code>, but not a
560request for <code>http://example.com/page.html</code>. If you have in 
561fact changed your document root - that is, if <strong>all</strong> of 
562your content is in fact in that subdirectory, it is greatly preferable 
563to simply change your <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>
564directive, or move all of the content up one directory,
565rather than rewriting URLs.</p>
566</dd>
567</dl>
568
569</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
570<div class="section">
571<h2><a name="fallback-resource" id="fallback-resource">Fallback Resource</a></h2>
572
573
574<dl>
575<dt>Description:</dt>
576<dd>You want a single resource (say, a certain file, like index.php) to
577handle all requests that come to a particular directory, except those
578that should go to an existing resource such as an image, or a css file.</dd>
579
580<dt>Solution:</dt>
581<dd>
582<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>
583
584<div class="example"><p><code>
585&lt;Directory /var/www/my_blog&gt;<br />
586<span class="indent">
587  FallbackResource index.php<br />
588</span>
589&lt;/Directory&gt;
590</code></p></div>
591
592<p>However, in earlier versions of Apache, or if your needs are more
593complicated than this, you can use a variation of the following rewrite
594set to accomplish the same thing:</p>
595
596<div class="example"><p><code>
597&lt;Directory /var/www/my_blog&gt;<br />
598<span class="indent">
599  RewriteBase /my_blog<br />
600<br />
601  RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f<br />
602  RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d<br />
603  RewriteRule ^ index.php [PT]<br />
604</span>
605&lt;/Directory&gt;
606</code></p></div>
607
608<p>If, on the other hand, you wish to pass the requested URI as a query
609string argument to index.php, you can replace that RewriteRule with:</p>
610
611<div class="example"><p><code>
612  RewriteRule (.*) index.php?$1 [PT,QSA]
613</code></p></div>
614
615<p>Note that these rulesets can be uses in a <code>.htaccess</code>
616file, as well as in a &lt;Directory&gt; block.</p>
617
618</dd>
619
620</dl>
621
622</div></div>
623<div class="bottomlang">
624<p><span>Available Languages: </span><a href="/en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
625</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
626<script type="text/javascript"><!--//--><![CDATA[//><!--
627var comments_shortname = 'httpd';
628var comments_identifier = 'http://httpd.apache.org/docs/2.2/rewrite/remapping.html';
629(function(w, d) {
630    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
631        d.write('<div id="comments_thread"><\/div>');
632        var s = d.createElement('script');
633        s.type = 'text/javascript';
634        s.async = true;
635        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
636        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
637    }
638    else { 
639        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
640    }
641})(window, document);
642//--><!]]></script></div><div id="footer">
643<p class="apache">Copyright 2013 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>
644<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
645if (typeof(prettyPrint) !== 'undefined') {
646    prettyPrint();
647}
648//--><!]]></script>
649</body></html>