converting links from MT to WordPress

I had over 1,000 posts in Movable Type that were archived individually, by month and by category. When I switched to WordPress I changed the format of the permanent links to include the date and title instead of the non-portable post ID (which, by the way, MT does not include when you export your blog so your permanent links won’t work if you export and import your blog through MT either).

I was concerned that I wouldn’t be able to get those old URLs to forward to WordPress but in the end it wasn’t all that difficult. The easiest was the monthly archive. It used to be “/archive/year_month.html” so I just added a line in the .htaccess to change it to “/archives/year/month” and I was done.

RewriteRule ^archive/([0-9]{4})_([0-9]{2}).html$ /index.php?year=$1&monthnum=$2

The category names would have been easy except that Movable Type uses underscores and WordPress uses hyphens. I already use PHP on my site, so I was able to take advantage of that fact. I found the code in WordPress that converts category names into what is used in the URL and put it in the category archive template. The code generates the new URL to use and forwards the user. This code must go at the very top of the template.

$title = strtolower( "<$MTArchiveTitle$>" );
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = preg_replace('/[^a-z0-9 _-]/', '', $title);
$title = preg_replace('/s+/', ' ', $title);
$title = str_replace(' ', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');
Header( "Location: /archives/category/$title" );

I did pretty much the same thing with Individual archives except I needed to add the date in addition to converting the title to the post slug. Here is the code I put at the top of the Individual archive template.

$title = strtolower( "<$MTArchiveTitle$>" );
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = preg_replace('/[^a-z0-9 _-]/', '', $title);
$title = preg_replace('/s+/', ' ', $title);
$title = str_replace(' ', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');
<MTDateHeader>
$date = "<$MTEntryDate format="%Y/%m/%d"$>";
</MTDateHeader>
Header( "Location: /archives/$date/$title" );

The .htaccess change is a little less ideal because it leaves the old URL. The other two changes change the URL in the user’s browser to the new link. The advantage however is that I was able to remove the category archives. I have to leave the rest of the files there because they contain the information needed to forward to the new URL.

Comments

 (Post a comment) | Comments RSS feed
  1. Dan: I came to you via the codex & hope you might have a few mins. to help me with my own Typepad to WP conversion. Carthik Sharma helped me with a lot of the intial stuff I needed to do to set up my WP blog by importing files, etc. He also spent a lot of time dealing with the link conversion issue & thought he had it licked. But either he didn’t or he did & something happened subsequent to that that made it stop working. Because my old TP post links are dead. Right now, they will take you to my new site but provide an error msg. saying the post was not found (that’s because it’s not converting the underscore to underline & ditching the .html suffix. And Carthik seems to be busy & hasn’t been able to help me w. this problem so I think I’m going to have to do it myself.

    I believe that Typepad would work similarly to MT & prob. use precisely the same code as you use above to get there.

    I can follow good directions involving installing code, but I’m not really a programmer. SO any help you can provide to me I’d appreciate.

    The codex directs you to add this code to the .ht access file:

    Redirect Permanent /archives/<$MTEntryID$>.html
    http://www.example.com/index.php?s=<$MTEntryTitle encode_url="1"$>

    First, where would I find this file? Is it within my wp folders/files? Does it matter where you place the code within the file?

    Above, you mention the Category Archive template. Where would I find that?

    If you think of anything else I should know in doing this pls. fire away…

    Comment by Richard Silverstein on June 12, 2005 @ 10:51 pm
  2. Richard: The .htaccess file is usually placed at the root of your site. You may already have one there, and it doesn’t matter where you place the code in the file as long as the forward rules don’t conflict with one another.

    The Category Archive template is a template type in Movable Type, so I’m guessing it’s the same within Typepad. It should be in the templates area.

    Best of luck to you.

    Comment by dan on June 13, 2005 @ 7:28 am

Comments are closed