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$ -->
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
14 http://www.apache.org/licenses/LICENSE-2.0
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.
23 <manualpage metafile="proxy.xml.meta">
24 <parentdocument href="./">Rewrite</parentdocument>
26 <title>Using mod_rewrite for Proxying</title>
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>
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>
46 <section id="dynamic-proxy">
48 <title>Proxying Content with mod_rewrite</title>
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>
65 <p>To simply map a URL to another server, we use the [P] flag, as
68 <highlight language="config">
70 RewriteBase "/products/"
71 RewriteRule "^widget/(.*)$" "http://product.example.com/widget/$1" [P]
72 ProxyPassReverse "/products/widget/" "http://product.example.com/widget/"
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>
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/"
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
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>