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