A Short CGI Script for Passing 404s to Another Server
By Silas Brown
There are several reasons why you might have more than one website. For example, you might have an account on a fast server with limited quota, and another on a server that is slower but has more space, and you keep certain rarely-used files on the slower server. Or you might have an easy-to-remember dyndns alias pointing to your home server, but some pages on another server that has a better connection but a longer address. If this is the case, then it might be useful to set one of the servers to forward any requests for pages it can't find to the other server. Then you can use this first server in all the addresses you type, and if the page requested is on it, it will be returned, otherwise the browser will be automatically redirected to check the second server for the same page. (Don't set that second server to redirect back to the first or you'll have an infinite loop.)
Using Apache, this can be accomplished as follows. Firstly, put the following line into your public_html/.htaccess file, replacing /~your-user-ID as appropriate (if you have your own hostname then you can omit it):
ErrorDocument 404 /~your-user-ID/cgi-bin/redirect.cgi
This tells Apache to run the script whenever it would normally issue a 404. If you want the redirection to take effect for index.html also (that is if index.html is missing), then you should also add:
Options -Indexes ErrorDocument 403 /~your-user-ID/cgi-bin/redirect.cgi
This prevents directory listing, and tells Apache to send the browser to the redirection script instead of showing the "Forbidden" message. This arrangement might be useful if your main site is on the other server but this server contains a few extra files that you want Apache to check for first.
Then create public_html/cgi-bin/redirect.cgi as follows, replacing other-site-address-goes-here and ~your-user-ID as appropriate:
#!/bin/bash echo Status: 301 Moved echo Location: "$(echo "$REQUEST_URI"|sed -e s,/,http://other-site-address-goes-here/, -e s,~your-user-ID/,,)" echo
Then chmod +x public_html/cgi-bin/redirect.cgi and test.
The above script should work regardless of whether or not ~your-user-ID is included in the request, that is, if Apache is serving your public_html on your own hostname then this script should work regardless of whether the incoming request is for your hostname or for your user ID on the main hostname.
Some antiquated browsers might not follow the 301 redirect. If this is a concern then you can add some text in the response as well. But the script as it stands is small and easily set up, and should work without trouble in most cases.
Talkback: Discuss this article with The Answer Gang
Silas Brown is a legally blind computer scientist based in Cambridge UK. He has been using heavily-customised versions of Debian Linux since 1999.