After upgrading to Movable Type 3.14, and before changing hosting, I really, really, needed to decruft the default URLs my old Movable Type installation used. Here is how I did it:
h3. Plugins needed: Key Values and MT-Regex
First, I installed Brad Choates’ “Key Values plugin”:http://bradchoate.com/weblog/2002/07/27/keyvalues – I then customized the display of Movable Type’s entry editing screen, so it displayed the “Keywords” field.
I also installed the “Regex Plugin”:http://bradchoate.com/weblog/2002/07/27/mtregex from Brad Choate, even if having it installed is *optional* for the scope of this document.
h3. Writing slugs: The Keywords field
After doing this, I proceeded to write manual slugs for each and every of the plus four hundred entries. The slug is written as a key-value pair. Example:
The goal of the slugs is to create good-looking and easily-typed URLs for an entry. For instance, this entry has the URL:
Since this step is quite labour-intensive, it should be noted that this step is *semi-optional* — If you have two different weblog entries in the same month that have the same title, you will *need to write a slug for at least one of them.*
When you write your slugs, you should restrict yourself to using the characters a-z, 0-9, –, _ and . My own slug writing strategy also excludes the underscore and the plus character. You should, even if permitted, avoid using capital letters, since most people who type URLs manually, have no notion that URLs are case sensitive, and type everything in lower-case.
h3. Changing the Archive URL strategy
I then changed the archive file naming strategy. This is done by going to “Weblog Config” → “Archive Files”.
My original “Individual Entry Archive” file template read
Depending on your own setup, the pad=”0″ section might be missing (you might want to make a mental noe of whether you use zero-padded URLs or not). I then changed this to:
The entire section needs to be on one line. You can download the “Individual Entry Archive filename template”:http://virtuelvis.com/download/2005/01/decrufting/archivefilename.tmpl
The other two archive types I use, are the monthly and category archives. The monthly archives previously read
, which I changed to
. Similarily the Category archive template read
which was changed to
h3. Setting up rewrites
I then created a new Index Template (“Templates” → “Create new index emplate”), with the output file set to archives/.htaccess (If your archive is located in a different directory, you might need to change this accordingly). This template has several purposes:
# It sets up the Default Type for files inside the /archvies/ directory to be text/html so that the crufty .html extension can be dropped.
# It sets up rewrites, so that when people try to access your old entries, for instance http://www.virtuelvis.com/archives/473.html, they will be sent to the new URL instead of the old one.
# Finally, a permanent rewrite is set up for any file ending in .html, so that visitors are sent to the proper archive URL. This means that when someone tries to access http://www.virtuelvis.com/archives/2005/01/index.html, they are redirected to http://virtuelvis.com/archives/2005/01/index instead.
bc.. DefaultType text/html
DirectoryIndex index index.html
Redirect permanent /archives/
RedirectMatch permanent /archives/(.*).html$
* *Step 1:* If you are using PHP files, you might want to change this section so your PHP parser is invoked.
* *Step 2:* The start and end tags for
MTEntries should be on separate lines. Everything in between *should be on one line*.
* *Step 3:* .html might need to be changed if you used another file extension in the past.
You can download the “.htaccess template”:http://virtuelvis.com/download/2005/01/decrufting/htaccess.tmpl
Set this template to be rebuilt automatically when index templates are rebuilt.
h3. Rebuild and test
By now, you are pretty much done. You should now *rebuild your entire Movable Type site.*
If everything went well, and you did not receive any errors during the rebuild, your Movable Type blog should now have user friendly URLs.
You should now test:
# Try accessing an individual entry archive by it’s old, numeric URL. You should be redirected to the new, cruft-free URL.
# Try accessing an individual entry archive by it’s new URL. You should end up on the correct page, and not receive any errors.
# Try accessing a category archive by it’s old URI with the .html extension. You should be redirected to the correct extensionless URL.
# Try accessing a category archive by it’s new URI. You should end up on the correct page, and not receive any errors.
# Repeat steps three and four, but for any of the date-based archives.
If everything worked well, you should now be able to do the following:
# Delete the old individual entry files, typically named 1.html and up.
# Delete the template used to create the .htaccess file. You should, however _not_ delete the htaccess file itself
# Delete any index.html files in your archive folder.
h3. Filename templates explained:
Create the appropriate directory for the files. If you use daily archives, you could optionally add /%d/ inside the format attribute:
Look in MTEntryKeywords for url key-value pairs:
Does the entry have a url key-value pair? If so, use this value for creating the filename:
If there is no manual URL specified in the keywords field of a weblog entry, use the munged entry title:
h3. Further expansions
If you installed the Regex plugin, it is possible to modify your other templates, so the index is stripped from links leading to any of your archive pages. I haven’t bothered, though.
There is one more thing you can do, and that is to create yearly archives, so your users can chop off the end of URLs and receive sensible pages instead of error messages or directory listings. This can be done with the “ArchiveYear”:http://mt-plugins.org/archives/entry/archiveyear.php plugin.
* “Cruft-Free URLs in Movable Type”:http://diveintomark.org/archives/2003/08/15/slugs — Mark Pilgrim
* “The Ultimate Weblogging System, outlined”:http://mpt.phrasewise.com/2003/05/02#a507 — Matthew Thomas
* “How to recognize a Weblog tool by its permalinks”:http://mpt.phrasewise.com/2003/07/26#a534 — Matthew Thomas