How can I redirect URLs using the proxy module in Apache?

Solution 1:

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, the RewriteRule will fire.
  • This rule substitutes /foo to http://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.

Solution 2:

If I understand you correctly, you probably want to look at: mod_proxy in combination with name-based virtual hosts

Here is a little example of what this might look like. All requests from the www.olddomain.com virtual host will be requested from www.newdomain.com and rewritten by apache:

NameVirtualHost *:80
<VirtualHost *:80>
     ServerName www.olddomain.com

     <Proxy *>
            Order deny,allow
            Allow from all
     </Proxy>
     ProxyPass / http://www.newdomain.com/
     ProxyPassReverse / http://www.newdomain.com/
     ProxyPassReverseCookieDomain www.newdomain.com www.olddomain.com 
</VirtualHost>