noizZze

Preserving Links When Moving From MovableType to Wordpress

So you plan on moving from MovableType to Wordpress for one or another reason. The Wordpress has an excellent Import function which lets you move all posts and comments from your MT to its database. The real problem is that the URLs of the posts won’t look the same. If you leave it this way, you will lose all the references to your posts. Here’s my complete recipe – the detailed step-by-step guide.

I found the solution for the BlogBridge site I was moving couple of weeks ago. The solution is complete and requires very little efforts. You will need:

  • FTP access to your MT script files
  • FTP access to your WP installation
  • The access to your MySQL database

Firstly, we need to patch a bit the MT posts export database. Along with all other information there should be the base name of a post exported. If your MT-style URL looks like below the base name is ‘history_of_chan’. We need to know it to be able to find the post by its base name later.

http://someblog.com/archives/2003/01/history_of_chan.php

No go to your MT installation and:

  1. Open a file {mt_home}/lib/MT/ImportExport.pm
  2. Find the line “DATE: <$MTEntryDate format=”%m/%d/%Y %I:%M:%S %p”$>”
  3. Add “BASENAME: <$MTEntryBasename$>” after it.

Secondly, we need to patch the WP a bit to support a new field.

  1. Connect to your MySQL database, select the database WP uses
  2. Run the script to create a table linking base names with posts: CREATE TABLE posts_basename (   id BIGINT(20) UNSIGNED PRIMARY KEY,   basename VARCHAR(50) NOT NULL );
  3. Open wp-admin/import/mt.php
  4. Find the line “$post = preg_replace(“|(—–\nBODY:.*)|s”, ”, $post);”
  5. Put this line after it “$post_basename = null;”
  6. Find the “switch ($key)” block several lines below
  7. Put this case-block right above the “case ‘AUTHOR’ :” case 'BASENAME' :   $post_basename = $value;   break;
  8. Scroll down to find this: // Add categories. if (0 != count($post_categories)) {   wp_create_categories($post_categories, $post_id); }
  9. Then, add this block after that one you found: // If post has a basename then we have to enter the entry into the map if ($post_basename != null) $wpdb->query(   "INSERT INTO posts_basename (id, basename) ".   "VALUES ($post_id, '$post_basename')");
  10. Now you can export your blog from MT and import it into WP. It’ll populate the database correctly.
  11. The next step is to create mt-old-post.php script in the root folder of your blog. Get this one and rename it. Don’t forget to tune the database settings and the link to your blog inside.
  12. You need to route the requests to old posts to this script and it will re-route them to the correct WP entries. You do this via updating .htaccess file (note that you don’t need the first two lines if they are already there, just put the third line after them; it’s also important that you didn’t put this third line at the end of the .htaccess file, because usually there’s a rule to route all unknown links to the home page of the blog and it will capture everything before it comes to your rule): RewriteEngine On RewriteBase / RewriteRule archives/[0-9]{4}/[0-9]{2}/(\w+)\.php$ mt_old_post.php?bn=$1 [QSA,L]

That’s all. Now you should have a WP blog with all your MT posts imported and all the old-style links preserved.