Movable Type Blog Migration
hatch
Over the last week, usually in the mid-to-late evenings — after Catherine falls asleep, I have been slowly migrating my B2-based blog to Movable Type.
I must say that for the most part the process has been fairly straight forward. The MT system installed smoothly and customizing the core MT templates, while time-consuming getting them to fit my old B2 template, were rather easy and extremely flexible.
However, during the migration process I had some interesting obstacles. In particular, I wanted to seamlessly maintain the entire URL-space of my old B2 blog with my new MT blog. My initial thinking was that with a little data-scrubbing and massaging I could export the MySQL table data from B2 and import the data into the MT table-space.
In addition, if I could retain the same entry/post IDs between the old and new system, I could easily redirect links via an Apache mod_rewrite regular expression mapping.
After some initial head-scratching, this idea was a bit more complex that I had thought given that I wanted to include comments as well. Plus, I wasn’t sure if retaining the entry/post IDs would break MT.
I did some quick searches via Google and the great MT Support forums and found Bill Grady’s excellent B2 Export script for MT, which allowed me to dump all of my B2 posts and comments into MT’s import format. This format enabled me to easily import my old post data into MT.
The problem however was that (as far as I can tell) MT’s import format does not allow for the specification of entry/post IDs, which excluded me from using a simple Apache mod_rewrite regular expression to map the URL-space.
Oh well, back to the drawing-board…
After further research, I found the following links regarding interesting solutions that utilize MT archive templates to create global redirects in Apache’s .htaccess or httpd.conf formats.
- Movable Type file rewrite
- Template Redirects — Another MT template solution that generates an .htaccess file
- Howto: Future-proof URLs in Movable Type
Unfortunately, these solutions used the entry_id as the key field in the mapping, which cased problems for me because my old B2 blog had post IDs that were inconsistent with MT — Plus, for some reason the post ID in B2 were out of order.
I though I could use the post date as my key field, but for some reason I found a number of inconsistencies between the post dates in the two data sets. Very odd.
Instead I used the entry title as my key field; this required me to insure that the entry titles between both old and new data sets where precisely the same and not contain any duplicates. This way I could use the entry titles to map old post IDs from B2 to the new URL space in Movably Type.
Once the titles were synchronized, I created an MT template to export my newly imported MT entries in a CSV format that I could manipulate in Excel. I used the following MT Archive template:
<mtentries lastn="999999">,,,
</mtentries>
I then export my B2 post data into a CSV file, sorted the data in Excel, by title; opened the newly-exported MT data in Excel and also sorted it by title. I now had two matching sets of data, each with unique entry/posting IDs. The next step was to construct the redirect mapping between old post ID and the entry’s new URL.
Ultimately, I used a bit of PHP to do the redirecting. I did this by constructing an associative array using the post ID from B2 as the index, with the MT entry URL as the value. I also utilized the ‘array_key_exists’ PHP function to determine if the old post ID was found in the array.
Here’s a snippet of code:
$entry_array = array (
"613" => "http://www.hatch.org/blog/2002/06/17/404.php",
"576" => "http://www.hatch.org/blog/2002/04/18/1000_ultrapersonal_computer.php");
// entry lookup $p = post_id
if ($p) {
if (array_key_exists($p, $entry_array)) { $url = $entry_array[$p]; } }
// redirect
header( "Location: ".$url );
Worked like a charm!
I wish I could use this or a similar technique to redirect my old RSS feed to my feed’s new location, but that’s a topic for another day…