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