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 https://dougal.gunters.org/b2rss.php RedirectPermanent /myphpblog/rss2.php https://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
https://dougal.gunters.org/myphpblog/archive.php?y=2003&m=09&d=05#pid273
- WordPress Permalink
https://dougal.gunters.org/index.php?m=200309#post273
- MyPHPBlog Comment
https://dougal.gunters.org/myphpblog/comments.php?eid=273
- WordPress Comment
https://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.
Pingback: #WordPress » dougal