summaryrefslogtreecommitdiff
path: root/docs/manual/rewrite/remapping.html.en
blob: 980678707509a3d8a05553b5ed825f1cd38295bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
              This file is generated from xml source: DO NOT EDIT
        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      -->
<title>Redirecting and Remapping with mod_rewrite - Apache HTTP Server</title>
<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
<link href="../images/favicon.ico" rel="shortcut icon" /></head>
<body id="manual-page"><div id="page-header">
<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>
<p class="apache">Apache HTTP Server Version 2.4</p>
<img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
<div id="path">
<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>
<div class="toplang">
<p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
</div>


<p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
<a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect and remap
request. This includes many examples of common uses of mod_rewrite,
including detailed descriptions of how each works.</p>

<div class="warning">Note that many of these examples won't work unchanged in your
particular server configuration, so it's important that you understand
them, rather than merely cutting and pasting the examples into your
configuration.</div>

</div>
<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>
<li><img alt="" src="../images/down.gif" /> <a href="#old-to-new-extern">Rewriting From Old to New (external)</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#movehomedirs">Resource Moved to Another Server</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for file extension change</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#browser-dependent-content">Browser Dependent Content</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#fallback-resource">Fallback Resource</a></li>
</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></div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>Assume we have recently renamed the page
      <code>foo.html</code> to <code>bar.html</code> and now want
      to provide the old URL for backward compatibility. However,
      we want that users of the old URL even not recognize that
      the pages was renamed - that is, we don't want the address to
      change in their browser.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>We rewrite the old URL to the new one internally via the
      following rule:</p>

<div class="example"><p><code>
RewriteEngine  on<br />
RewriteRule    ^<strong>/old</strong>\.html$  <strong>/new</strong>.html [PT]
</code></p></div>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="old-to-new-extern" id="old-to-new-extern">Rewriting From Old to New (external)</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>Assume again that we have recently renamed the page
      <code>foo.html</code> to <code>bar.html</code> and now want
      to provide the old URL for backward compatibility. But this
      time we want that the users of the old URL get hinted to
      the new one, i.e. their browsers Location field should
      change, too.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>We force a HTTP redirect to the new URL which leads to a
      change of the browsers and thus the users view:</p>

<div class="example"><p><code>
RewriteEngine  on<br />
RewriteRule    ^<strong>/foo</strong>\.html$  <strong>bar</strong>.html  [<strong>R</strong>]
</code></p></div>
</dd>

<dt>Discussion</dt>

    <dd>
    <p>In this example, as contrasted to the <a href="#old-to-new-intern">internal</a> example above, we can simply
    use the Redirect directive. mod_rewrite was used in that earlier
    example in order to hide the redirect from the client:</p>

    <div class="example"><p><code>
    Redirect /foo.html /bar.html
    </code></p></div>

    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="movehomedirs" id="movehomedirs">Resource Moved to Another Server</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>If a resource has moved to another server, you may wish to have
      URLs continue to work for a time on the old server while people
      update their bookmarks.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>You can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect these URLs
      to the new server, but you might also consider using the Redirect
      or RedirectMatch directive.</p>

<div class="example"><h3>With mod_rewrite</h3><p><code>
RewriteEngine on<br />
RewriteRule   ^/docs/(.+)  http://new.example.com/docs/$1  [R,L]
</code></p></div>

<div class="example"><h3>With RedirectMatch</h3><p><code>
RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1
</code></p></div>

<div class="example"><h3>With Redirect</h3><p><code>
Redirect /docs/ http://new.example.com/docs/
</code></p></div>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>How can we transform a static page
      <code>foo.html</code> into a dynamic variant
      <code>foo.cgi</code> in a seamless way, i.e. without notice
      by the browser/user.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>We just rewrite the URL to the CGI-script and force the
      handler to be <strong>cgi-script</strong> so that it is
      executed as a CGI program.
      This way a request to <code>/~quux/foo.html</code>
      internally leads to the invocation of
      <code>/~quux/foo.cgi</code>.</p>

<div class="example"><p><code>
RewriteEngine  on<br />
RewriteBase    /~quux/<br />
RewriteRule    ^foo\.<strong>html</strong>$  foo.<strong>cgi</strong>  [H=<strong>cgi-script</strong>]
</code></p></div>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for file extension change</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>How can we make URLs backward compatible (still
      existing virtually) after migrating <code>document.YYYY</code>
      to <code>document.XXXX</code>, e.g. after translating a
      bunch of <code>.html</code> files to <code>.php</code>?</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>We rewrite the name to its basename and test for
      existence of the new extension. If it exists, we take
      that name, else we rewrite the URL to its original state.</p>

<div class="example"><p><code>
#   backward compatibility ruleset for<br />
#   rewriting document.html to document.php<br />
#   when and only when document.php exists<br />
&lt;Directory /var/www/htdocs&gt;<br />
<span class="indent">
RewriteEngine on<br />
RewriteBase /var/www/htdocs<br />
<br />
RewriteCond $1.php -f<br />
RewriteCond $1.html !-f<br />
RewriteRule ^(.*).html$ $1.php<br />
</span>
&lt;/Directory&gt;
</code></p></div>
    </dd>

    <dt>Discussion</dt>
    <dd>
    <p>This example uses an often-overlooked feature of mod_rewrite,
    by taking advantage of the order of execution of the ruleset. In
    particular, mod_rewrite evaluates the left-hand-side of the
    RewriteRule before it evaluates the RewriteCond directives.
    Consequently, $1 is already defined by the time the RewriteCond
    directives are evaluated. This allows us to test for the existence
    of the original (<code>document.html</code>) and target
    (<code>document.php</code>) files using the same base filename.</p>

    <p>This ruleset is designed to use in a per-directory context (In a
    &lt;Directory&gt; block or in a .htaccess file), so that the
    <code>-f</code> checks are looking at the correct directory path.
    You may need to set a <code class="directive"><a href="../mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> directive to specify the
    directory base that you're working in.</p>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>



      <dl>
        <dt>Description:</dt>

        <dd>The goal of this rule is to force the use of a particular
        hostname, in preference to other hostnames which may be used to
        reach the same site. For example, if you wish to force the use
        of <strong>www.example.com</strong> instead of
        <strong>example.com</strong>, you might use a variant of the
        following recipe.</dd>

        <dt>Solution:</dt>

        <dd>

<p>The very best way to solve this doesn't involve mod_rewrite at all,
but rather uses the <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code>
directive placed in a virtual host for the non-canonical
hostname(s).</p>

<div class="example"><p><code>
&lt;VirtualHost *:80&gt;<br />
<span class="indent">
  ServerName undesired.example.com<br />
  ServerAlias example.com notthis.example.com<br />
<br />
  Redirect / http://www.example.com/<br />
</span>
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost *:80&gt;<br />
<span class="indent">
  ServerName www.example.com<br />
</span>
&lt;/VirtualHost&gt;
</code></p></div>

<p>You can alternatively accomplish this using the
<code class="directive"><a href="../mod/core.html#if">&lt;If&gt;</a></code>
directive:</p>

<div class="example"><p><code>
&lt;If "%{HTTP_HOST} != 'www.example.com'"&gt;<br />
<span class="indent">
Redirect / http://www.example.com/
</span>
&lt;/If&gt;
</code></p></div>

<p>Or, for example, to redirect a portion of your site to HTTPS, you
might do the following:</p>

<div class="example"><p><code>
&lt;If "%{SERVER_PROTOCOL} != 'HTTPS'"&gt;<br />
<span class="indent">
Redirect /admin/ https://www.example.com/admin/
</span>
&lt;/If&gt;
</code></p></div>

<p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
- if, for example, you need this to work with a larger set of RewriteRules -
you might use one of the recipes below.</p>

<p>For sites running on a port other than 80:</p>
<div class="example"><p><code>
RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]<br />
RewriteCond %{HTTP_HOST}   !^$<br />
RewriteCond %{SERVER_PORT} !^80$<br />
RewriteRule ^/?(.*)         http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]
</code></p></div>

<p>And for a site running on port 80</p>
<div class="example"><p><code>
RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]<br />
RewriteCond %{HTTP_HOST}   !^$<br />
RewriteRule ^/?(.*)         http://www.example.com/$1 [L,R,NE]
</code></p></div>

        <p>
        If you wanted to do this generically for all domain names - that
        is, if you want to redirect <strong>example.com</strong> to
        <strong>www.example.com</strong> for all possible values of
        <strong>example.com</strong>, you could use the following
        recipe:</p>

<div class="example"><p><code>
RewriteCond %{HTTP_HOST} !^www\. [NC]<br />
RewriteCond %{HTTP_HOST} !^$<br />
RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]
</code></p></div>

    <p>These rulesets will work either in your main server configuration
    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>
        </dd>
      </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>A particular resource might exist in one of several places, and
      we want to look in those places for the resource when it is
      requested. Perhaps we've recently rearranged our directory
      structure, dividing content into several locations.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>The following ruleset searches in two directories to find the
      resource, and, if not finding it in either place, will attempt to
      just serve it out of the location requested.</p>

<div class="example"><p><code>
RewriteEngine on<br />
<br />
#   first try to find it in dir1/...<br />
#   ...and if found stop and be happy:<br />
RewriteCond         %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI}  -f<br />
RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1  [L]<br />
<br />
#   second try to find it in dir2/...<br />
#   ...and if found stop and be happy:<br />
RewriteCond         %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI}  -f<br />
RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1  [L]<br />
<br />
#   else go on for other Alias or ScriptAlias directives,<br />
#   etc.<br />
RewriteRule   ^  -  [PT]
</code></p></div>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
    <p>We have numerous mirrors of our website, and want to redirect
    people to the one that is located in the country where they are
    located.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
    <p>Looking at the hostname of the requesting client, we determine
    which country they are coming from. If we can't do a lookup on their
    IP address, we fall back to a default server.</p>
    <p>We'll use a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
    directive to build a list of servers that we wish to use.</p>

<div class="example"><p><code>
HostnameLookups on<br />
RewriteEngine on<br />
RewriteMap    multiplex         txt:/path/to/map.mirrors<br />
RewriteCond  %{REMOTE_HOST}     ([a-z]+)$ [NC]<br />
RewriteRule   ^/(.*)$  ${multiplex:<strong>%1</strong>|http://www.example.com/}$1  [R,L]
</code></p></div>

<div class="example"><p><code>
##  map.mirrors -- Multiplexing Map<br />
<br />
de        http://www.example.de/<br />
uk        http://www.example.uk/<br />
com       http://www.example.com/<br />
##EOF##
</code></p></div>
    </dd>

    <dt>Discussion</dt>
    <dd>
    <div class="warning">This ruleset relies on
    <code class="directive"><a href="../mod/core.html#hostnamelookups">HostNameLookups</a></code>
    being set <code>on</code>, which can be
    a significant performance hit.</div>

    <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
    directive captures the last portion of the hostname of the
    requesting client - the country code - and the following RewriteRule
    uses that value to look up the appropriate mirror host in the map
    file.</p>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="browser-dependent-content" id="browser-dependent-content">Browser Dependent Content</a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
      <p>We wish to provide different content based on the browser, or
      user-agent, which is requesting the content.</p>
    </dd>

    <dt>Solution:</dt>

    <dd>
      <p>We have to decide, based on the HTTP header "User-Agent",
      which content to serve. The following config
      does the following: If the HTTP header "User-Agent"
      contains "Mozilla/3", the page <code>foo.html</code>
      is rewritten to <code>foo.NS.html</code> and the
      rewriting stops. If the browser is "Lynx" or "Mozilla" of
      version 1 or 2, the URL becomes <code>foo.20.html</code>.
      All other browsers receive page <code>foo.32.html</code>.
      This is done with the following ruleset:</p>

<div class="example"><p><code>
RewriteCond %{HTTP_USER_AGENT}  ^<strong>Mozilla/3</strong>.*<br />
RewriteRule ^foo\.html$         foo.<strong>NS</strong>.html          [<strong>L</strong>]<br />
<br />
RewriteCond %{HTTP_USER_AGENT}  ^<strong>Lynx/</strong>         [OR]<br />
RewriteCond %{HTTP_USER_AGENT}  ^<strong>Mozilla/[12]</strong><br />
RewriteRule ^foo\.html$         foo.<strong>20</strong>.html          [<strong>L</strong>]<br />
<br />
RewriteRule ^foo\.html$         foo.<strong>32</strong>.html          [<strong>L</strong>]
</code></p></div>
    </dd>
  </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>



<dl>
 <dt>Description:</dt>

   <dd>
     <p>On some webservers there is more than one URL for a
     resource. Usually there are canonical URLs (which are be
     actually used and distributed) and those which are just
     shortcuts, internal ones, and so on. Independent of which URL the
     user supplied with the request, they should finally see the
     canonical one in their browser address bar.</p>
   </dd>

   <dt>Solution:</dt>

     <dd>
       <p>We do an external HTTP redirect for all non-canonical
       URLs to fix them in the location view of the Browser and
       for all subsequent requests. In the example ruleset below
       we replace <code>/puppies</code> and <code>/canines</code>
       by the canonical <code>/dogs</code>.</p>

<div class="example"><p><code>
RewriteRule   ^/(puppies|canines)/(.*)    /dogs/$2  [R]
</code></p></div>
        </dd>

     <dt>Discussion:</dt>
     <dd>
     This should really be accomplished with Redirect or RedirectMatch
     directives:

     <div class="example"><p><code>
     RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2
     </code></p></div>
     </dd>
      </dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>

  

  <dl>
    <dt>Description:</dt>

    <dd>
<p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
of the webserver directly relates to the URL "<code>/</code>".
But often this data is not really of top-level priority. For example,
you may wish for visitors, on first entering a site, to go to a
particular subdirectory <code>/about/</code>. This may be accomplished
using the following ruleset:</p>
</dd>

    <dt>Solution:</dt>

    <dd>
      <p>We redirect the URL <code>/</code> to
      <code>/about/</code>:
      </p>

<div class="example"><p><code>
RewriteEngine on<br />
RewriteRule   <strong>^/$</strong>  /about/  [<strong>R</strong>]
</code></p></div>

<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>

<div class="example"><p><code>
RedirectMatch ^/$ http://example.com/about/
</code></p></div>

<p>Note also that the example rewrites only the root URL. That is, it
rewrites a request for <code>http://example.com/</code>, but not a
request for <code>http://example.com/page.html</code>. If you have in
fact changed your document root - that is, if <strong>all</strong> of
your content is in fact in that subdirectory, it is greatly preferable
to simply change your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
directive, or move all of the content up one directory,
rather than rewriting URLs.</p>
</dd>
</dl>

</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="fallback-resource" id="fallback-resource">Fallback Resource</a></h2>


<dl>
<dt>Description:</dt>
<dd>You want a single resource (say, a certain file, like index.php) to
handle all requests that come to a particular directory, except those
that should go to an existing resource such as an image, or a css file.</dd>

<dt>Solution:</dt>
<dd>
<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>

<div class="example"><p><code>
&lt;Directory /var/www/my_blog&gt;<br />
<span class="indent">
  FallbackResource index.php<br />
</span>
&lt;/Directory&gt;
</code></p></div>

<p>However, in earlier versions of Apache, or if your needs are more
complicated than this, you can use a variation of the following rewrite
set to accomplish the same thing:</p>

<div class="example"><p><code>
&lt;Directory /var/www/my_blog&gt;<br />
<span class="indent">
  RewriteBase /my_blog<br />
<br />
  RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f<br />
  RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d<br />
  RewriteRule ^ index.php [PT]<br />
</span>
&lt;/Directory&gt;
</code></p></div>

<p>If, on the other hand, you wish to pass the requested URI as a query
string argument to index.php, you can replace that RewriteRule with:</p>

<div class="example"><p><code>
  RewriteRule (.*) index.php?$1 [PT,QSA]
</code></p></div>

<p>Note that these rulesets can be used in a <code>.htaccess</code>
file, as well as in a &lt;Directory&gt; block.</p>

</dd>

</dl>

</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
</div><div id="footer">
<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>
<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>
</body></html>