301

Firstly, my biggest problem was I used WordPress for my old blog. This site runs on Textpattern (for now), so all my old post archive links from WordPress don’t match the same format as the their Textpattern alternatives.

Here’s an example. When someone visits an article from my old site, they might hit up the following address:

http://www.cssdev.com/archives/2006/03/19/css-tweak/

Now, in Textpattern this same article can be found at a much simpler link:

http://www.blazenewmedia.com/articles/css-tweak/

So, how can I make my old WordPress installation forward people to a new Textpattern formatted link, while still saving my search engine rankings, and ensuring the new page gets re-indexed in the same position?

301 Redirects to the Rescue

By sending a “301: Moved Permanently” header to the browser, it’s possible to tell anyone who visits my old WordPress installation that the content has moved for good.

Not only that, search engine spiders will follow this redirect and re-index all of my pages in place of the old ones. This preserves search engine rankings and prevents people from seeing a whole bunch of dead links when they find my old site on Google.

To get this to work with WordPress, all I had to do was place a few lines of PHP code into the “index.php’ file in the root of the site. This file is the hub that all WordPress pages run through.

Here’s the PHP code I used to send a 301 redirect:

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.blazenewmedia.com/blog/");
die;

That solved redirecting, but that didn’t solve my problem with incompatible URLs between WordPress and Textpattern.

To fix this problem, I needed to grab the page that the user had requested, reformat it so that it matched Textpattern’s formatting, then forward them to the new link.

This was done with the following code:

/**
 * Get the requested page: eg /archives/2006/03/19/css-tweak/
 */
$requestedPage = $_SERVER['REQUEST_URI'];

/**
 * Split the requested page
 * into and array of sections
 * using the "/" as a splitter.
 */
$reqArray = explode("/", $requestedPage);

/**
 * Check to see if the second, third
 * and fourth sections (starts at 0)
 * are numbers, if they are we can
 * assume these are dates.
 */
if( is_numeric($reqArray[1]) &&
    is_numeric($reqArray[2]) &&
    is_numeric($reqArray[3]) ) {

 /**
  * Send the correct 301 header.
  */
 header("HTTP/1.1 301 Moved Permanently");

 /**
  * Send them to the new textpattern
  * formatted URL. We know that the last
  * section of the URL is the same (/css-tweak/)
  * so we can append that to the end of the URL.
  */
 header("Location: http://www.blazenewmedia.com/articles/" . $reqArray[4]);
 die; // stop anything else from executing.
}

I’ve had the new site up for just over a week now. Google has already almost completely re-indexed my new site. The links to my old CssDev site are reducing by the day. Soon, all the old CssDev links will be completely converted over to Blaze. It works!

If you’re looking to change your domain, or move your site, 301’s are definitely the way to go. With a little PHP knowledge you can also handle redirecting old bookmarked links, even if the formatting has changed. For me, 301’s have worked flawlessly.