An all to common error on the web is broken links. You end up with broken links from other sites
whenever you rearrange your site. People bookmark some page they like and come back 3 months later
only to find a ‘404 Not Found’, giving them no help whatsoever about where to start looking for the
page you originally had on your site. Let’s solve this, or atleast be friendly to your users and
help them get back on track whenever they hit ‘a 404’. You can even create a common page to report all
errors you might encounter while rendering your pages.
whenever you rearrange your site. People bookmark some page they like and come back 3 months later
only to find a ‘404 Not Found’, giving them no help whatsoever about where to start looking for the
page you originally had on your site. Let’s solve this, or atleast be friendly to your users and
help them get back on track whenever they hit ‘a 404’. You can even create a common page to report all
errors you might encounter while rendering your pages.
PHP together with Apache gives you quite alot of freedom to create your own error pages, but requires
some reconfiguring and a tiny bit of coding. Let’s start off with the configuration part.
some reconfiguring and a tiny bit of coding. Let’s start off with the configuration part.
The Apache ErrorDocument
directive specifies what document (or URI) Apache should redirect a user to in case of an error.
It allows you to specify one resource for each and every error code one of your users might run into.
Start off by adding a
users that ask for a page that does not exist to the ‘error.php’ page you will soon write. Don’t
forget to restart Apache for the changes to take effect.
directive specifies what document (or URI) Apache should redirect a user to in case of an error.
It allows you to specify one resource for each and every error code one of your users might run into.
Start off by adding a
ErrorDocument 404 /error.php
directive to your server configuration. This will redirectusers that ask for a page that does not exist to the ‘error.php’ page you will soon write. Don’t
forget to restart Apache for the changes to take effect.
Next, we write up a very simple error.php:
The file you requested (<?=$REDIRECT_URL?>) does not exist on this server.
Please look for the page you wanted at <A HREF="/">the front page</A>.
Now try to access a page that doesn’t exist on your server, and voila, you’re at the error.php page
with a nice and friendly message and a link to your front page!
with a nice and friendly message and a link to your front page!
Let’s extend this. As you can see, I used the REDIRECT_URL variable on the error.php page. This
is a variable that Apache sets whenever it invokes an ErrorDocument directive, and gives you
a possibility to find the originating resource whenever there’s an error. Apache also sets a number
of other variables in this case, all of them
documented here. Use these variables to create a nice error page that gives your users
a nice and friendly error page instead of the good ol’ boring Apache default page.
is a variable that Apache sets whenever it invokes an ErrorDocument directive, and gives you
a possibility to find the originating resource whenever there’s an error. Apache also sets a number
of other variables in this case, all of them
documented here. Use these variables to create a nice error page that gives your users
a nice and friendly error page instead of the good ol’ boring Apache default page.