How can I redirect URLs using the proxy module in Apache?
From your comment on my previous answer I gather that you are using Apache as a forwarding proxy (ProxyRequests On
). You can use mod_rewrite
to proxy pass through specific URL's.
You probably got something like this in your Apache config:
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Allow from xx.xx.xx.xx
</Proxy>
Then you have to add the following in order to proxy-pass all requests from www.olddomain.com/foo
to www.newdomain.com/bar
:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule /foo(.*)$ http://www.newdomain.com/bar/$1 [P,L]
What this does is:
- When a request is made to the host
www.olddomain.com
, theRewriteRule
will fire. - This rule substitutes
/foo
tohttp://www.newdomain.com/bar/
. - The substitution is handed over to
mod_proxy
(P
). - Stop rewriting (
L
).
Example result:
- Browser is configured to use your Apache as proxy server.
- It requests
www.olddomain.com/foo/test.html
. - Your Apache will rewrite this to
www.newdomain.com/bar/test.html
. - It will request this page from the responsible web server.
- Return the result to the browser as
www.olddomain.com/foo/test.html
.