How to redirect your 404 error to a custom page
A 404 error message is the standard HTTP standard response code which is returned when the visitor cannot communicate with the server. This is a very common error on the web and it occurs when you are trying to visit a page which has either been deleted or has been moved somewhere else. For example, if you change the structure of your website and move a certain directory to a different part of your site, anyone trying to visit the old page url will get a 404 error message.
A 404 error message usually looks something like this :
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a mod_bwlimited/1.4 PHP/5.2.6 Server at yourwebsite.com Port 80
If a visitor comes to your site and sees a standard 404 error message it’s unlikely they will make the effort to see any part of your site. Therefore it is very important to create a 404 page on your site and redirect traffic from incorrect urls.
Thankfully, htaccess makes this very easy. First of all you need to create a 404 error page. So for example, you would create a page at http://www.yoursite.com/404.php which says something like :
It appears you are looking for something which isn’t there. Either you have entered an incorrect URL or we have messed up. Why not visit our home page or alternatively, search for what you are looking for in the search box below.
Whilst a 404 error page does not send the visitor to the exact page they want, it does point them in the right direction and it means they are more likely to stay on your site.
Once you have your 404 page setup, all you need to do is send visitors to incorrect url’s to this page. To do this just add the following line to your .htaccess file :
ErrorDocument 404 /404.php
You can place the 404 error template anywhere you want. For example you could place all error messages in a folder called errormessages
ErrorDocument 404 /errormessages/404.php
That’s all there is to it. Now when a visitor views an incorrect url on your site they will see your custom message.