Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Redirection

I mentioned earlier that I have converted my site to WordPress, and set up link redirection for the old links. There’s some discussion going on over at Alex’s site about this subject as well. I figured I’d put up some notes about what I did.

First of all, for a couple of static links, like for my RSS files, I just added some redirect directives to my .htaccess file, like so:

# redirect the old MyPHPBlog RSS locations to the new WordPress ones:
RedirectPermanent /myphpblog/rss.php http://dougal.gunters.org/b2rss.php
RedirectPermanent /myphpblog/rss2.php http://dougal.gunters.org/b2rss2.php

That part was easy enough. But the hard part is redirecting old permalinks and comment links. Both systems use querystring parameters, and they use different variable names and formats, plus the MPB stuff was all under a subdirectory:

MyPHPBlog Permalink
http://dougal.gunters.org/myphpblog/archive.php?y=2003&m=09&d=05#pid273
WordPress Permalink
http://dougal.gunters.org/index.php?m=200309#post273
MyPHPBlog Comment
http://dougal.gunters.org/myphpblog/comments.php?eid=273
WordPress Comment
http://dougal.gunters.org/index.php?p=273&c=1#comments

The first thing that comes to mind is to use Apache’s mod_rewrite functionality. However, as others have noted, mod_rewrite doesn’t have access to the query string. My URI rewriting needs were also more complicated than Matt’s, so I decided to borrow some ideas from php.net’s url shortcuts.

I do this by using a custom 404 error handler in my .htaccess:

ErrorDocument 404 /redirector.php

Then, I wrote a redirector program in PHP that gets the name of the requested file and the querystring variables, and sends the browser a permanent redirect, or a real 404, as necessary. It should be fairly easy for me to extend the redirector as necessary to accomodate other URLs, if needed.

About Dougal Campbell

Dougal is a web developer, and a "Developer Emeritus" for the WordPress platform. When he's not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.
This entry was posted in Tech and tagged , , . Bookmark the permalink.

2 Responses to Redirection

  1. Pingback: #WordPress » dougal

Leave a Reply to Chris Cancel reply

%d bloggers like this: