#native_company# #native_desc#
#native_cta#

Redirecting on Page Not Found (404)

By PHP Builder Staff
on April 17, 2012

Hi tim, love your site.

Here’s my small contribution, hope you enjoy it. I found it quite useful and it’s so simple that shows off the power and easiness of using php3+apache.

Feel free to edit it at will.
Cheers,
Joao Gonçalves

In my University we had the main web server and several virtual hosts on the same machine as user’s homepages, but now we are changing the main server and some of the virtual servers to a brand new machine which would get the DNS record of ‘www’ that is also used by users.

So I was facing a problem, the hostname used was going to change to another machine and several users had sent URLs with the ‘www’ hostname and it would break alot of bookmarks to change this. So, how to make this as transparent as possible?

The solution became quite simplish. Error handling! In the new server all i had to do was to redirect to the old server when the error was 404 (File not found). It was quite simplish to do a one liner in PHP3 that would do the job. Simply create an entry in your httpd.conf saying:

ErrorDocument 404 /redir-404.php3

Then create on your DocumentRoot redir-404.php3:

<?php

        $redir = getenv(“REDIRECT_URL”);
        header(“Location: http://web2.your-domain.com/$redir”);

?>

People won’t come cheering up on you, since is transparent for the users but you avoid quite a mess 🙂

Just don’t do this same scheme on the second web server, preferably don’t try to put this one redirecting to the first one, you’ll get some loops and unpredicted behaviour.

Hope this small hack finds itself useful.

Apache ErrorDocument Run-time directive:

http://www.apache.org/docs/mod/core.html#errordocument