]> granicus.if.org Git - apache/blob - docs/manual/rewrite/proxy.xml
Quote path/URL arguments to Proxy* directives.
[apache] / docs / manual / rewrite / proxy.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <manualpage metafile="proxy.xml.meta">
24   <parentdocument href="./">Rewrite</parentdocument>
25
26 <title>Using mod_rewrite for Proxying</title>
27
28 <summary>
29
30 <p>This document supplements the <module>mod_rewrite</module>
31 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
32 how to use the RewriteRule's [P] flag to proxy content to another server.
33 A number of recipes are provided that describe common scenarios.</p>
34
35 </summary>
36 <seealso><a href="../mod/mod_rewrite.html">Module documentation</a></seealso>
37 <seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
38 <seealso><a href="remapping.html">Redirection and remapping</a></seealso>
39 <seealso><a href="access.html">Controlling access</a></seealso>
40 <seealso><a href="vhosts.html">Virtual hosts</a></seealso>
41 <!--<seealso><a href="proxy.html">Proxying</a></seealso>-->
42 <seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
43 <seealso><a href="advanced.html">Advanced techniques</a></seealso>
44 <seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
45
46 <section id="dynamic-proxy">
47
48   <title>Proxying Content with mod_rewrite</title>
49
50   <dl>
51     <dt>Description:</dt>
52
53     <dd>
54     <p>
55     mod_rewrite provides the [P] flag, which allows URLs to be passed,
56     via mod_proxy, to another server. Two examples are given here. In
57     one example, a URL is passed directly to another server, and served
58     as though it were a local URL. In the other example, we proxy
59     missing content to a back-end server.</p>
60     </dd>
61
62     <dt>Solution:</dt>
63
64     <dd>
65       <p>To simply map a URL to another server, we use the [P] flag, as
66       follows:</p>
67
68 <highlight language="config">
69 RewriteEngine  on
70 RewriteBase    /products/
71 RewriteRule    ^widget/(.*)$  http://product.example.com/widget/$1  [P]
72 ProxyPassReverse "/products/widget/" "http://product.example.com/widget/"
73 </highlight>
74
75    <p>In the second example, we proxy the request only if we can't find
76    the resource locally. This can be very useful when you're migrating
77    from one server to another, and you're not sure if all the content
78    has been migrated yet.</p>
79
80 <highlight language="config">
81 RewriteCond %{REQUEST_FILENAME}       !-f
82 RewriteCond %{REQUEST_FILENAME}       !-d
83 RewriteRule ^/(.*) http://old.example.com/$1 [P]
84 ProxyPassReverse "/" "http://old.example.com/"
85 </highlight>
86     </dd>
87
88     <dt>Discussion:</dt>
89
90     <dd><p>In each case, we add a <directive
91     module="mod_proxy">ProxyPassReverse</directive> directive to ensure
92     that any redirects issued by the backend are correctly passed on to
93     the client.</p>
94
95     <p>Consider using either <directive
96     module="mod_proxy">ProxyPass</directive> or <directive
97     module="mod_proxy">ProxyPassMatch</directive> whenever possible in
98     preference to mod_rewrite.</p>
99     </dd>
100   </dl>
101
102 </section>
103
104 </manualpage>