As mentioned in my previous entry, I converted davidchiu.net from MT to WP. The migration was pretty easy and I just followed the instructions I found on Wordpress.org. The only thing is that I didn’t care for their solution for redirecting previous MovableType permalinks to new Wordpress pages. So I came up with my own solution: create a custom WP 404 page which redirects the user to a search results page. The effect would be this:
If a user comes from an old MT permalink such as http://davidchiu.net/2003/05/some_old_title.php that does not correspond to an existing Wordpress page, convert the filename to a title fragment (ie, some_old_title.php becomes “some old title”) and return a Wordpress search result.
This should take care of all old permalinks. The corresponding code is pretty simple. I just added the following code to the top of my custom 404 page:
<?php
$filename=basename($_SERVER['REQUEST_URI'],”.php”);
$filename=basename($filename,”.html”);
$title=ereg_replace(’_|-’,’ ‘,$filename);
header(”Location: http://”.$_SERVER['SERVER_NAME'].”/?s=\”".$title.”\”");
?>
The first two lines strip either the “.html” or “.php” extension from the original request URL. The ereg_replace() function converts any underscores or dashes to spaces. Finally, the header() function redirects the user to the Wordpress search result page for the title fragment.
In my initial tests it works great. Let me know if this code could be better.
0 Responses to “Preserving Permalinks From a MovableType to Wordpress Conversion”
Leave a Reply