Proxy a web site and change the content on the fly

Posted on the October 10th, 2008 under Computers,Linux by Martin Bergek

Apache can do just about everything with a bit of configuration. I have used HTTP proxying on a number of occasions to make content from one site appear to come from another site. This can be very handy for sites I host at home where I only have one IP address but two servers and want to host different web sites on the same public IP – and all of them on the standard HTTP port.

However, until recently I hadn’t experienced that Apache can also rewrite the content in the actual response. I was faced with the requirement to change all the URIs in the proxied web site so that they pointed to the new URL. When I first searched the net I found the module mod_proxy_html which sounded like the way forward. In the end I didn’t go that route since I found it overly complex and didn’t transform all instances of the links. Instead, the solution was simple and used another Apache module – mod_substitute.

The following virtual host configuration sets up a proxy so that the site www.one.com is proxied to www.two.com. In addition, any occurrences of www.one.com is changed into www.two.com:


<VirtualHost *>
  ServerAdmin postmaster@two.com
  ServerName www.two.com
  <Proxy *>
    Order deny,allow
  </Proxy>
  ProxyRequests on
  ProxyPass / http://www.one.com/
  ProxyPassReverse / http://www.one.com/
  AddOutputFilterByType SUBSTITUTE text/html
  Substitute "s|www.one.com|www.two.com|n"
</VirtualHost>

Share

One Response to 'Proxy a web site and change the content on the fly'

  1. August 10, 2010 at 10:15
    mask IP