Michael : this won't work... i dunno why, but on my server, it doesn't... the http referer variable isn't set...
you have to do something like this :
in your .htaccess file, you put :
ErrorDocument 404 /redirect.php
(so, without the http, because you don't want a redirecttion)
then, in your redirect.php, you can use the $REQUEST_URI to redirect to the right page...
for instance: someone goes to http://domain.tld/whatever
then $REQUEST_URI will contain "/whatever". get rid of the "/", then select the right URL from the database, and do a http redirect to that URL - this should work fine.
hope this helps,
fallus