News Archive
PhpRiot Newsletter
Your Email Address:

More information

How to Avoid 404s and Redirect Old URLs in PHP

Note: This article was originally published at Planet PHP on 3 August 2010.
Planet PHP

Nothing can be said to be certain, except death and taxes. And URL changes.

It's often necessary to reorganize your site and change the URL structure but, assuming you have similar content, users should rarely encounter a aopage not founda error. Producing unnecessary 404 pages is one of my top 10 development mistakes.

In this article, we'll create an automated PHP redirection system that converts old URLs to a new address. It's not production code, but it will illustrate the basics so that you can adapt it for your own website.

1. Create a 404 error-handling file

If you're yet to have a aonot founda page, create a basic one named 404.php in the root of your website:

Page not found

Page not found

Sorry, we cannot find that page.

Please return to the home pageā€¦

Note: What's a 404?

404 is the HTTP error number returned when a resource is unable to be located on the server. The PHP code at the top of the above file returns this code to ensure systems such as search engines don't mistake the page for real content.

2. Configure your server

You now need to tell your server that all 404 errors should be handled by the 404.php file. If you're using Apache, add the following line to an .htaccess file in the root of your website:

errordocument 404 /404.php

For IIS, open the Internet Information Services Manager. In IIS7, double-click the aoError Pagesa icon. (Users of previous versions must select the aoCustom Errorsa tab of the website properties.) Edit the 404 error code, choose a type of aoURLa, and enter ao/404.phpa as the address.

If you now visit a nonexistent page, such as, you should see the error page we created above.

3. Create the redirection system

We'll place our redirection code in another file named redirect.php, to keep the functionality separate from the 404 content.

Add the following code at the top of your 404.php file just after the include('redirect.php');

Now create redirect.php in the website root and add the following code:

The current page address is stored in $oldurl, e.g. /non-existent.url.

We now need to examine that address and, if possible, translate it to a new URL (stored in the $newurl variable). How this is achieved will depend on the structure of your old and new URLs. For example, if the only change is that your ablog' folder has been renamed ablogs,' the following code could be sufficient:

$newurl = str_replace('blog', 'blogs', $oldurl);

You may be able to use a series of substitutions or regular expressions. Alternatively, a mapping of every old URL to its new address could be defined in an array or database table.

If you have fairly simple redirect requirements, the following code could be used:

$redir = array('blog' = '/blogs/', 'video' = '/videos/', 'demo' = '/demonstrations/main/'); while ((list($old, $new) = each($redir)) && !$newurl) { if (strpos($oldurl, $old) !== false) $newurl = $new; }

The $redir array defines a number of value pairs that can be configured accordingly. If the first string can be found anywhere within the old URL, the redirection address is set to the second string. In the example above, if the word ablog' is found in the missing page URL, the user will be redirected to a/blogs/'. If the old URL contains two or more of those words, the first one takes precedence; for example, /video/demonstrations would find avideo' first and redirect to a/videos/'.

Once you have a $newurl value, you could optionally double-check that it exists before redirecting. Unfortunately, that poses a few risks:

  • The new URL may lack a physical file; for example, you're using WordPress permali

Truncated by Planet PHP, read more at the original (another 2454 bytes)