As you may or may not know, I am in the process of switching from Movable Type to WordPress. I want to make sure that any old links stay up and redirect to the new ones, but I ran into a bit of a dilemma. Originally, my format for permalinks was “/entries/2004/05/22/04.33.24.php” (totally based on the date… May 22nd, 2004 at 4:33:24 AM, in that case.) But later on, I decided to use the Keywords field for making post slugs (short versions of the title, used in the URL.) So if I entered a Keyword slug, the entry would be something like “/entries/2004/05/22/this_entry_slug.php”
So my earlier permalinks don’t have slugs… something I couldn’t really fix without a lot of messy work in MT. But now that I am moving to WordPress, and am going to slightly change my URL scheme anyway, I could go back and give all those posts slugs of their own.
But if I assign the slugs in MT then import, my old MT links will be invalid. And if I do it in WP, there’s no way for me to redirect MT links to WP ones (because I don’t know what slug I’ll choose.)
So then I thought… what do imported posts from another blogging system have in common? Post slugs are formatted differently in WP than in MT… basing links on titles is sometimes okay, but not when the title is long and makes for a cumbersome URL. But one thing they do have in common is their timestamp. That’s the key. No matter what blogging system you switch to, your timestamps will stay the same.
I’m not actually using this, as WordPress work isn’t complete… but here’s how it works.
Movable Type Individual Entry Archive Template:
$wp_url = '<$MTEntryDate format="?year=%Y&monthnum=%m&day=%d&hour=%H&minute=%M&second=%S"$>'; header("HTTP/1.1 301"); header("Location: http://txfx.net/wp_directory/" . $wp_url); exit();
This is just a PHP 301 permanent redirect script. The URL to which it redirects is just your main WordPress directory, with the year, month, day, hour, minute, and second of the post passed as variables.
WordPress will display the post that matches these criteria, which will be the correct post. The only problem is that the URL looks like crap, and you’re not showing off your fancy new permalinks. So this code near the top of your main WordPress template (right after require('./wp-blog-header.php');
) will take care of that.
if($year && $monthnum && $day && $hour && $minute && $second && $posts){ $redirect_url = get_permalink(); header("HTTP/1.1 301"); header("Location: $redirect_url"); exit(); }
This little thing checks to see if the year, month, day, hour, minute and second are all set, as well as making sure that a post matches those parameters. If one does, it gets the pretty permalink for it, and performs another 301 redirect. The intermediate step is transparent… if someone enters the permalink of an old MT entry, they’ll end up at the new WP version with the new WP permalink proudly displayed in their address bar. And Google will love it.
Edit: I used %I in the Movable Type date format for the hour, which is the 12-hour-clock version. The correct one to use is H, the 24-hour-clock version. Funny how I didn’t catch that, as I tested it on a few of my posts. Maybe it’s because 90% of my posts are at 3am. Yeah, that’s probably it.
Posted by Mark @ 2:44 am in “Weblogs”Permalink | Add a comment