Preserving Permalinks

This may come in handy for anyone else who has migrated to Wordpress from Moveable Type.

I didn’t really want to lose all those incoming clicks from the old style permalinks which have been long since spidered so I set about figuring out the best way to forward the old style to the new style.

There are some mod_rewrite examples on the Wordpress site but they don’t take into consideration that Wordpress also uses .htaccess mod_rewrite for ’search engine friendly’ links.

Here’s my final solution which is in two parts, one is a modification to the .htaccess file which converts the old “/archive/2007/04/blog_entry_title.html” format into “findmt.php?s=blog_entry_title”. A new file”findmt.php” queries the database and forwards to the correct blog entry.

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*archives/.*/(.*).html /findmt.php?s=$1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

findmt.php

require( 'wp-config.php' );

//-----------------------------------------
// Fix up S parameter
//-----------------------------------------

$search = strtolower( trim( urldecode( $_REQUEST['s'] ) ) );
$search = str_replace( ‘_’, ‘-’, $search );
$search = preg_replace( ‘/[^\d\w-]/’, ”, $search );

//-----------------------------------------
// Get DB connection
//-----------------------------------------

$connection_id = @mysql_connect( DB_HOST, DB_USER, DB_PASSWORD );

if ( ! $connection_id )
{
print "Could not connect to the DB";
exit();
}

if ( ! mysql_select_db( DB_NAME, $connection_id) )
{
print "Could not select the DB";
exit();
}

//-----------------------------------------
// Attempt to get post via title.
//-----------------------------------------

$qid = mysql_query( "SELECT * FROM " . $table_prefix . "posts WHERE LOWER(post_name)='" . addslashes( $search ) ."'" );

if ( $row = mysql_fetch_array( $qid, MYSQL_ASSOC ) )
{
header("Location: /index.php?p=" . $row['ID'] );
}
else
{
header(”Location: /index.php” );
}

?>

nicely done.
was there any problem converting the post from moveable type to wordpress?
i mean dB and info wise.

“nicely done.
was there any problem converting the post from moveable type to wordpress?
i mean dB and info wise.”

Nope, Wordpress did all the hard stuff.

“you geek.”

Yes. Yes I am.

*
To prove that you're not a bot, enter this code
Anti-Spam Image