Redirecting old blog URLs

September 1, 2007 – 11:32 pm

During the switch from Movable Type to WordPress I decided to change the URL structure of this blog. The old category style archive pages from my Movable Type system were in the /archives directory and the permalinks for my individual posts included the date. The URL for this post would have been like so:

/archives/2007/09/01/redirecting_old_blog_urls.php

Now my permalinks are much more simple, like this:

/redirecting-old-blog-urls/

Also, my category archives are now in a directory called /category. Well, they’re not actually in that directory physically the way they were with Movable Type, but WordPress works it’s magic to make it seem that way.

Those changes would not have been very friendly to anyone that ended up on my blog via an old link, whether it be from someone else’s blog or from a search engine. That visitor would have seen a generic 404 page and probably would have clicked their back button and went about their browsing. I like having visitors to my blog and I want them all to enjoy their stay here and eventually come back so I had to deal with my new URLs somehow.

To do this, I have created a system of 301 redirects using a combination of .htaccess and PHP. The first part, the .htaccess is pretty simple. It only deals with fixing the /archive to /category change. It goes like this:

Redirect 301 /archives/ http://sixtimeseight.com/category/

Pretty straight forward.

A bit more tricky is the completely new URL style of my permalinks. For that I decided to turn to PHP. If I’m not mistaken, virtually every page of a WordPress powered website is served via good ol’ /index.php. Given that, I figured I could write a quick little addition to that file and everything would be just fine. Here’s what I came up with:


if(strpos($_SERVER['REQUEST_URI'],'category/2005') || strpos($_SERVER['REQUEST_URI'],'category/2006') || strpos($_SERVER['REQUEST_URI'],'category/2007')){
$thisPage = $_SERVER["REQUEST_URI"];
$parts = explode('/', $thisPage);
$fileName = $parts[count($parts) - 1];
$fileName = str_replace('_','-',$fileName);
$fileName = str_replace('.php','/',$fileName);
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://sixtimeseight.com/'.$fileName);
exit;
}

My blog only goes back to 2005, so I just look in the URL for any of the past three years (including 2007) and if one of them is in there, my little script does it’s thing and sends the browser to the new URL. I included the ‘category’ in front of the years just in case I ever decide to include any of those three numbers in a post title. If I did that my script would get confused and try to redirect itself to itself over and over and your browser wouldn’t be happy with that at all.

To figure out the new URL, we create an array of the URL parts using the / as the separator. From that array, we find the last part of the URL which would be the old file name from the Movable Type system. That’s all I need since I have removed the dates from my URLs. With that file name, we replace all the underscores with dashes, then replace the .php at the end with a slash. Then we tell the browser that this newly created string is where the post has been moved to and mark it as a 301 (permanent redirect) which will (hopefully) alert the search engines to update their indexes.

I’m certain that there is a more efficient way of achieving the same effect, but I didn’t really feel like digging into it that deeply. This works quite well and doesn’t really add a noticeable amount of overhead to the script execution time, so it’s ok with me.

There are still some small details to work out. Some of my old archives had underscores in the URLs so I may end up moving the archive redirect from .htaccess and including that in the PHP as well.

Post a Comment