I used to spit out individual entry archives into files named after each entries id.

For example, entry 300 was located at /blog/archives/entries/0003000/. I’ve since moved to using the entry title as the file name, so I have to figure out how to redirect the old links to the new locations. The easiest way to do this seems to be a quick and dirty mod_rewrite and a CGI script.

Here’s the RedirectMatch directive:

RewriteEngine   on
RewriteRule     ^/blog/archives/entries/([0-9]{1,6})(/|\.xsp)$  /cgi-bin/mt-byid.cgi?entry_id=$1 [R=301,L]

And here’s the CGI itself:

#!/usr/bin/perl -wT
use strict;

    if ($0 =~ m!(.*[/\\])!) {
        $MT_DIR = $1;
    } else {
        $MT_DIR = './';
    unshift @INC, $MT_DIR . 'lib';
    unshift @INC, $MT_DIR . 'extlib';
    push @INC, $MT_DIR . 'plugins';

eval {
        use CGI;
        use MT;
        use MT::Entry;

        my $permalink;
        my $query = CGI->new;
        my $entry_id = $query->param('entry_id');

        if ($entry_id !~ /^\d{1,6}$/) {
                $entry_id = 0;

        my $mt = MT->new;
        my $entry = MT::Entry->load({id => $entry_id});
        if ($entry) {
                $permalink = $entry->permalink('Individual');
        } else {
                $permalink = '/404.icf';

        print $query->redirect( -uri=>$permalink,
                                -status=>'301 Moved Permanently'

if ($@) {
    print "Content-Type: text/html\n\n";
    print "Got an error: $@";
See more posts about: perl | All Categories