Categories
Computers Linux

Proxy a web site and change the content on the fly

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>

Categories
Computers Linux

Combine subversion, WebSVN and a web page in one Apache site

In my last post I wrote about how to get libapache_mod_auth_pam to play nicely with Apache2. This post is about putting it all together, complete with WebSVN and a default web page so that one can write an introductory text to the users using the site.

Getting subversion up and running on Apache was explained in the last post. The problem with that, however, is that the Location directive means that all content on the site is directed to dav_svn. Hence, it is not possible to access an index.html page in the root, nor the /websvn folder. If you try, you will just get the message “Could not open the requested SVN filesystem”.

Of course, the simple solution to this would be to put the dav_svn module one folder down in the site structure (e.g. http://svn.example.com/repositories/<repository>) by changing the Location directive to <Location /repositories/>. But that is ugly.

I assume there are many ways to solve this but here is how I did it.

  1. First, create two sites – svn.example.com and websvn.example.com (or whatever you want to call them).
  2. Configure the site svn.example.com as explained in the previous post
  3. Configure the site websvn.example.com as you would any other static site, adding a index.html file in the root with whatever content you want to have there
  4. Now, before the Location directive in the svn.example.com site definition, add the following:

  5. RewriteEngine on
    RewriteRule ^/$ index.html [R]
    ProxyPass /websvn/ http://websvn.example.com/websvn/
    ProxyPassReverse /websvn/ http://svn.example.com/websvn/
    ProxyPass /index.html http://websvn.example.com/index.html

  6. Finally, you may also want to add SSL support for the svn.example.com site but I won’t go into that here

To make this work, a couple of Apache2 modules must be enabled, namely: proxy, proxy_http and rewrite.

What the above does is that it rewrites accesses to the root URL / to /index.html. Then, all requests to either /index.html or /websvn are proxied to the other site (which does not have dav_svn enabled so they work fine there).

With everything in place, you should be able to access http://svn.example.com for the index.html page, http://svn.example.com/websvn for the WebSVN interface to the repositories – and anything else for the real repositories.

A prettier solution would be if the Location directive for Apache supported negated regular expressions but I have come to the conclusion that it doesn’t – at least not the version I am using (2.2.8).

css.php