This article will help developers who wish to create a new web site layout. Once you have read this, you will be able to redirect users to the new pages created via a 404 error page. Read more.
Have you ever designed your website, built the structure and decided one day that you want to completely redesign your layout? Well, if you have an established website, changing your structure can be a real pain for your users or people searching for your site in search engines. This tutorial will show you how to avoid getting those Error 404 Not Found errors and redirect people back into your new design.
Let's say that we have a few files such as this:
Your Site Root /index.php /page1.php /page2.php /page3.php
Your new layout schema that you decided to use will change to this:
Your Site Root /index.php /articles.php (Was page1.php) /images.php (Was page2.php) /guestbook.php (Was page3.php)
We will assume that you are going to remove the old files after you do your site design. When your site was running before you had about 10 websites linking to your pages and you had submitted everything to search engines. When people click on your old links, they'll get an Error 404 / Document Not Found error message. Let's avoid getting that message and direct the users to your index to avoid any traffic loss.
Utilizing Apache's built in custom error document handling you can setup your own error 404 document that redirects the user to your index page and gives them a message as well as displays your index page. The first thing we need to do is build our error handler. I use a php script that includes my index.php file, gives the user a message at the top and the re-directs them to the main page after 2 seconds. Here's how:
<?php //errror404.php echo "<font color="#FFFF00" size="3"><center><strong>You have reached a page that has changed or no longer exists. You have been redirected to our index page. Please update your bookmarks!</strong></center></font>"; //Meta refresh to send them back to index.php echo "<meta http-equiv="refresh" content="2;URL=/index.php">"; //include the index.php so that we can give them something to look at besides an ugly error include 'index.php'; ?>
There you have it! A simple php script that gives you a good looking Error 404 page and redirects the user to your index page. On the next page, we'll show you how to setup a htaccess file that allows Apache to use this new error404.php script.
The next step requires you to either have administrative access to your Apache web server's configuration files or send an email to your admin and ask them to allow overrides for your website. A directive should be added to your httpd.conf for the directory which your website lives in. Let's say for this example it's /www/htdocs. Here's a line in your httpd.conf that you must have for this .htaccess file to work:
Once you have that done, you'll need to create a file called ".htaccess" and place it inside your website's directory. Here's how simple this .htaccess file is:
ErrorDocument 404 /error404.php
Wow that was simple eh? After you have completed all of the above steps, you can now test it out. Let's call up http://www.yourdomain.com/foo.php (Where foo.php is a file that does not exist). You should be directed to your index page and see a message that appears at the top of the screen for approximately 2 seconds and then the user will be directed back to your index.php file.
Congratulations, You've successfully redirected all broken links on your website back to your index! You will no longer lose traffic due to random clean-ups of your files and site changes. Just don't delete the error404.php because that would be too ironic that you can't even display your own error document!
Here's a few more custom error documents that you can make for your website. Just use the same methods above that we used for creating these new pages. If you were really cool, you could setup a switch inside your error404.php file and manage all of your custom errors in one file. Here's the list:
Error in Client 400 Bad syntax 401 Unauthorized 402 Not Used (Payment Granted) 403 Forbidden 404 Not Found
Error in Server 500 Internal Error 501 Not Implemented 502 Overloaded 503 Gateway Timeout
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.