Partial domain redirection with Apache

Most web hosting services, even the free ones, are a lot faster than your home computer can be, but they often lack the flexibility you want, for example to support cgi-bin scripting.

If you use Apache to host a domain on your home computer, you can redirect the static part of your website (including the pictures!) to an external web server and service the rest on your own computer, with something like this:

	<VirtualHost 212.120.112.48>
	ServerName gay.vanrein.org
	ScriptAlias /cgi-bin/ /var/www/gay/cgi-bin/
	DocumentRoot /var/www/redirect.php
	</VirtualHost>

This configuration redirects normal traffic to another URL through the redirect.php script included below. This is the normal behaviour, overridden only for paths starting with /cgi-bin/ according to the ScriptAlias directive.

To make this scheme work, all script references must be made as a full URL, like this: http://gay.vanrein.org/cgi-bin/stuurkaart. One way of doing that is to include the following line in the HEAD section of all your static HTML pages:

	<BASE HREF="http://gay.vanrein.org/path/to/file.html" TARGET="_top">

To round it up, here is the redirect.php code for this setup:

	<HTML>
	<HEAD>
	<TITLE>Page forwarded by dns.vanrein.org</TITLE>
	<BASE TARGET="_top">
	</HEAD>
	<FRAMESET ROWS="100%,*" FRAMEBORDER=NO BORDER=0>
	<?php

		$redir['phd.vanrein.org'] = 'http://wwwhome.cs.utwente.nl/~vanrein';
		$redir['rick.vanrein.org'] = 'http://home.zonnet.nl/vanrein';
		$redir['gay.vanrein.org'] = 'http://huizen.dds.nl/~vanrein';

		$new = $redir [$SERVER_NAME] . $PATH_INFO;
		print "<FRAME NAME='Page forwarded' SRC='$new' NORESIZE>\n";

	?>
	</FRAMESET>
	</HTML>

Needless to say, Apache allows far more complicated options. For example, the ScriptAliasMatch directive may well serve you to host certain extensions (such as .php) on your own computer.

This idea is based on the plain redirection services as offered for free by CapiBara and MyDomain; these services don't include this more active approach.


Page maintained by Rick van Rein, comments welcomed.