Brad Feld

Back to Blog

Permalink Changes in Movable Type

Jun 13, 2007
Category Writing

In general, Movable Type has worked great for me across the various blogs I maintain although I periodically fantasize about switching to WordPress.  However, during an upgrade along the way, we accidentally switched the name mapping from text to numbers.  We figured out a hack for this, but Ross (my IT guy) finally sat down and figured out the correct solution.  The following is the best approach we could figure out for anyone that wants to figure out how to “remap MT URLs.”  If you are not an MT nerd, please punt on this post. Oh, and if you subscribe via RSS and got duplicate updates for all the recent posts this is why – since the URL’s changed the feed got updated (sorry about that.)

A while back Brad asked me about changing the way Movable Type generates the permalinks and category pages. Somehow when we migrated from one server to another we changed the way we generated the URLs. Rather than the URLs being the nice pretty version of the post (where it’s based on the name of the post) we ended up using a numbering system. Brad noticed this and did a little research on how to change it. He quickly passed it off to me so I could figure out how to make this change to get us to the nice pretty URLs we’re using now. Since it took me a while to get this all figured out I figured I’d share what I went through.

Apache Redirection (mod_rewrite)
One of the first things we realized was that we would have to keep the old pages in some way so that all the information indexed by the search engines would still work. We couldn’t just dump all the old pages and go with the new ones otherwise all the old links (from search engines as well as from other bloggers) would break. The best way to achieve this is to use mod_rewrite. I won’t take the time to go into Apache mod_rewrite here, there are plenty of resources on the net for that. Once I had it up and running (it’s included by default in SuSE) I had to generate the mappings for the old pages to the new ones. This is what took so long.

Permalinks in Movable Type
When I started looking into this I found lots of sites with information on how to generate a file that would build all the redirection for me (details below). You could create a small template that would generate both the ID for the post and the long name for the post. I worked with this for days off and on and kept running into the same problem – the long URLs didn’t match what Movable Type was really generating in the end making this worthless. Since Brad has over 1500 posts there was no way for me to do this manually, it had to be automated.

This morning I hit my eureka moment – I need to change the way the pages were being generated by Movable Type. Then and only then would my redirection file create the right URL’s for me. In Movable Type I went to Settings – Publish and changed the Archive Types to their defaults (the first selection in the drop down list). Remember we’d custom created these at some point (I’m not sure why, when, or how.) Once these were set correctly Movable Type knew how to generate all the proper URLs

Creating the Redirection Template
The next step was to create a simple template to do the mapping for me. I’d found some examples out there and after playing with it for a while I settled on the template below. To create this template click on “Templates” then “Create New Index Template”. Name it whatever you want (I used migrate.html) and paste in the code below. Save and rebuild the template then hit migrate.html to see your results. If everything worked correctly you should see the mappings in your browser. Don’t forget to update the path at the beginning of the statement below to match the path for your site, as it will most likely differ.

<MTEntries sort_order=”ascend”>
redirect /blog/archives/<$MTEntryID pad=”1″$>.html <$MTEntryPermalink archive_type=”Individual”$><br>
</MTEntries>

In the redirection statement you want the “old” or missing page to be first and the new, correct one second. Notice that the second one is the full URL including the domain (you’ll see this in your generated output.) Next you’ll need to use the output to create your .htaccess file. Create the .htaccess file at the root of your website (or root of your blog.) All you should have to do is copy the contents of the template above (the generated template) into this .htaccess file and do some testing. For Brad’s site I removed all the old archive files first so I would be certain that I was getting the new pages not the old ones, confirming that the redirection was working properly.

Final Thoughts
I have a few obvious pieces of final advice. BACK UP YOUR SITE FIRST. Yes, yes, this is obvious, or should be but don’t be lazy with this step. My backups saved me at least three times during this process. Second test like crazy. This site has over 1500 articles so I can’t test them all but I tested a big sampling of pages to be sure I got it all right. If you find something that isn’t working as you think it should, please feel free to email me and let me know.