Create a 404 error handler with Movable Type
Between switching from one CMS to another or upgrading, you may run into a situation where you stand to lose your original URL structure. For example, if you are upgrading from older versions of Movable Type, you may use the underscore for your URLs but want to convert to a hyphen. In even older installations, entry ID numbers were used as the url (ex. /2009/03/001383.html). With Movable Type, it's easy to build an error handler that will convert from one URL format to another.
There are two files which will be created, a .htaccess file and a PHP script which will do the conversion for you between old URLs and new URLs when the old URL is accessed. Figure 1 is the contents of the .htaccess file needed to set up the HTTP 404 error handling. Figure 2 is the basic Movable Type error handler template.
ErrorDocument 404 /404errorhandler.php
-Figure 1
<?php
$mappings = array(
<mt:Entries lastn="2500">
"/<mt:EntryDate format="%Y/%m"/>/<mt:EntryBasename separator="_"/><mt:BlogFileExtension/>" =>
"<mt:BlogURL/><mt:EntryDate format="%Y/%m"/>/<mt:EntryBasename separator="-"/><mt:BlogFileExtension/>"
<mt:unless name="__last__">,</mt:unless>
</mt:Entries>
);
$location = ($mappings{$_SERVER{'REQUEST_URI'}} != '' ?
$mappings{$_SERVER{'REQUEST_URI'}} : 'http://www.yourdomain.com');
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '. $location);
?>
-Figure 2
You can upload the .htaccess file by FTP to the root of your domain. It does not need to be created as a Movable Type template. To create the PHP error handler, go to Design->Templates, create a new index template, use the contents from figure 2 (or a variation thereof that is appropriate for your old URLs) and save the template as /404errorhandler.php
That shows a simple conversion between underscores and hyphens. To convert between ID numbers and the entry basename, make the following substitution.
"/<mt:EntryDate format="%Y/%m"/>/<mt:EntryBasename separator="_"/><mt:BlogFileExtension/>"
Becomes:
"<mt:EntryDate format="%Y/%m"/>/<mt:EntryID/><mt:BlogFileExtension/>"
To convert from WordPress or another CMS or blogging application that uses log entry names, go to Preferences->Entry. You will see a field for basename length. Increase it to at least 100 characters in length. This must be done before you import your entries into Movable Type, as Movable Type will not rebuild entry basename fields once they have already been created. If you import all of your WordPress entries while the length is set to the default value of 40 characters, they will be permanently set to that.
Adjust the first part of the error handler template to match the URL structure that you are coming from, import your entries, and then republish the entire blog. Once that is done, you are all set.
References:
Leave a comment