Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This seems like a super-basic question but I am having a hard time tracking down a straightforward solution, so appreciate any help and patience with me on this:

I want to configure my Apache proxy server to redirect certain URLs so that, for example, a web browser HTTP request for www.olddomain.com gets passed to the proxy server which then routes the request to www.newdomain.com which sends a response to the proxy server which then passes it back to the web browser.

Seems so simple, yet I don't see how to achieve this on Apache. I know Squid/Squirm offer this functionality so am guessing I am missing something really basic. I know I can use RewriteRule to dynamically modify the URL and pass it to the proxy server, but I effectively want to do the reverse, whereby the proxy server receives the original URL, applies the RewriteRule, and then forwards the HTTP request to the new URL.

Hope that makes sense. Thanks in advance for any help.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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 to 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 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 webserver
  • Return the result to the browser as www.olddomain.com/foo/test.html
share|improve this answer
    
Perfect, this is exactly what I wanted to achieve. Only issue with your recommendation is that I needed to use a '%' instead of a '$' in front of '{HTTP_HOST}' in order for RewriteCond to trigger. Thanks much! –  LearningIT Apr 29 '10 at 8:19
add comment

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>
share|improve this answer
    
Thanks much, but I was targeting a different scenario. Here is what I want to enable: a) browser makes HTTP request for www.olddomain.com/foo, which passes to proxy server for handling b) proxy server sees www.olddomain.com/foo and based on a pre-determined mapping, forwards the HTTP request to www.newdomain.com/bar instead c) proxy server receives HTTP response from www.newdomain.com/bar and forwards to browser (which basically thinks it just received an HTTP response to its HTTP request to www.olddomain.com/foo) Does that clarify? –  LearningIT Apr 28 '10 at 2:59
    
For the sake of self-documentation, if the www part is omitted in the ProxyPassReverseCookieDomain directive, the domains should start with a dot. –  Bram Schoenmakers Jun 4 '10 at 11:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.