Leo's MovableType Tips

by Leo A. Notenboom

by Ask Leo!

Changing Page Names Mid-Stream

Ask Leo! was originally set up to name files using MovableType 2.x's default structure. Individual articles were archived with old style names of the form: http://ask-leo.com/archives/000001.html

That works, but current thinking is that search engines "like" seeing keywords in URLs. Since I wanted to do all I could to improve my search engine rankings, I decided to change my page names from being numbered and in a subdirectory to being the article title and in the root of the site.

Changing names midstream becomes a bit of a challenge. With several hundred articles on the site already, I wanted to switch to the new naming scheme while still allowing the old-style URLs, that people had perhaps bookmarked, to work.

I'm presenting these steps in the order that you need to do them to a live, running site. Review the entire set of steps before proceeding. If you follow these steps in this order, your site should never appear "down" and links will not be broken as you make this conversion.

Step 1 - Create a Mapping Index Template

We'll start by creating some support infrastructure for the name change. The first is the tool that will allow the old names to be redirected to the new names.

The first step is creating a new template that actually writes a tiny program in PHP. This program maps the internal article number to the new "dirified" name.

In List Templates, create a new Index Template, called ArticleMap:

<?php
<MTEntries lastn='10000'>
$rgEntries[<$MTEntryID$>] = "<$MTEntryPermalink$>";
</MTEntries>
?>

Yes, that is the entire template. If your MT site has more than 10,000 articles, then feel free to increase the "lastn" number.

Configure its output file to be "articlemap.php".

This is an index template... meaning that it has access to all of the entries, and is regenerated each time an article is added or modified.

Rebuild this template. You'll see that the created articlemap.php contains a series of entries like this:

$rgEntries[1] = "http://ask-leo.com/archives/000001.html";

With the PHP indicators "<?php" at the top and "?>" at the bottom.

Step 2 - Create the Redirector

Create a new file "byid.php" in the root of your site, and place into it the following:

<?php
include "articlemap.php";
$szLoc = "http://ask-leo.com";
$id = $_GET['id'] + 0;
if ("" != $rgEntries[$id])
$szLoc = $rgEntries[$id];
header ("location: $szLoc");
?>

(You can also download this from http://mttips.com/files/byid.zip).

This tiny PHP program uses the article map we created in Step 1. It takes as a parameter an article number, looks up that article number in the mapping table, and redirects to the corresponding page.

Replace the "ask-leo.com" in the script with the location you want visitors to be redirected to should the look-up fail. As you can see I typically use the home page, but you could redirect to an error page, or your website's 404 page.

Try it now. Try this link: http://ask-leo.com/byid.php?id=1, but:

You should be redirected to the permalink for the article number you specified. (If you use the link as presented above, you'll go to the article that was originally Ask Leo! article #1.)

Step 3 - Rewrite the URLs

Now that we have a script that will take an ID and redirect it to the "right" page, we need to transform the incoming requests for what will become the old style URLs using URL Rewriting.

Our goal is that an incoming request that looks like this:

http://ask-leo.com/archives/000001.html

should be processed as:

http://ask-leo.com/byid.php?id=000001

That's accomplished with the following URL Rewrite:

RewriteRule /([0-9]+)\.html$ /byid.php?id=$1 [PT]

In English, that rule says that any incoming page request whose filename is a series of numbers followed by ".html" be changed to a request for "byid.php" with the "id" parameter set to equal the string of numbers. The string of numbers, of course, is the MovableType ID for the article being requested.

So now, let's make sure that works. Did you notice that we didn't include the word "archive" in the rewrite test? That's on purpose, because it means that this should now also work:

http://ask-leo.com/000001.html

Remember to use your own site, and your own article ID for that test. Even though there is no 000001.html in the root of your site, the rewrite to use byid.php will trigger, and redirect you to the correct filename.

Step 4 - Turn on 'dirified' Filenames

OK, now it's time to change the filenames that MovableType saves your articles as.

In your weblog config, under the Publishing tab, in the Archive Mapping section (formerly the Archiving tab), set the Individual Entry Archive, Archive File Path to:

<$MTEntryTitle dirify="1"$>.html

Individual Entry File Path

MovableType uses this field as a kind of "mini template" for the filename to be used for each entry. Be careful not to introduce any additional characters, such as trailing spaces, because that will confuse both MovableType and you.

Setting this field instructs MovableType to use the "dirified" entry title (taking out or altering characters that would cause problems in filenames) plus ".html" as the filename to use for each article.

If it's possible, you may have more than one article with the same title, then you can make sure that each article's filename is unique by including the ID in the filename as well:

<$MTEntryTitle dirify="1"$>_<$MTEntryID$>.html

Since no two entries will have the same ID number, even if they have the same title, this approach will guarantee that they have unique filenames.

NOW... the first scary test.

Rebuild your site. All of it.

Here is what will happen:

All other references throughout your MovableType weblog will also simply use the new article file names.

Note that we're still using "archives" in the resulting URL. The last step gets rid of that too.

Step 5 - Move your archives to the root

The default in MovableType 2.x was to place articles in a subdirectory off of the root named "archives". I wanted my articles to be placed in the root of my site instead (the theory being that files in the root might be perceived as "more important" to the search engines).

In your weblog settings, under the Publishing tab (formerly the Core Setup tab), remove "/archives" from the Archive Root (formerly the Local Archive Path), and the Archive URL.

For example:

/home/httpd/vhosts/ask-leo.com/html/archives

Becomes

/home/httpd/vhosts/ask-leo.com/html

And:

http://ask-leo.com/archives/

becomes simply

http://ask-leo.com/

Together, those two settings instruct MovableType to place the files in the desired location - the root of the site - and generate any references to them in that location as well.

Time for the scary test again.

Rebuild your site. All of it.

Here's what will happen this time:

All other references throughout your MT weblog that MT generates will also simply use the new article file names in their new location.

One you have this in place, you're done. You could stop at this point, if you wished. You've successfully renamed and moved your articles, and no visitors noticed while you were doing it.

But there is still a little clean up, and one more step that I have found pretty handy:

Step 6 - Rewrite the simplest URLs

This step is entirely optional, and falls into the category of "while you're in there anyway ..."

One side effect of using article titles in their URLs is that the URLs can get kind of long. That's ok when the URL is being generated automatically and referenced in web pages such as the rest of your MT website. But if you want to perhaps send someone a pointer to a specific article, it can be rather cumbersome.

We've seen already that the existing URL Rewrite allows us to still reference an article as

http://ask-leo.com/0000001.html

Well, it turns out that this also works:

http://ask-leo.com/1.html

Try it on your own site. Now, wouldn't it be handy to get rid of the ".html"? Wouldn't it be nice if you could simply point people at:

http://ask-leo.com/1

Because of everything we've done so far, we have all the pieces in place. Just add one more URL Rewrite rule to make it happen:

RewriteRule /([0-9]+)$ /byid.php?id=$1 [PT]

This redirects all URLs ending in "/" followed by a number to byid.php with that number as the id parameter.

Step 7 - Clean up your archives

Even though we've moved our entries out of the archives directory, we've not actually deleted any of the files therein. That's been kind of a safety precaution so far. If, for example, the rewrite rules failed to operate, those files would still be there for your visitors to access.

But now that you've moved everything and verified that it's all working properly, you can remove the archives and free up the space associated with them.

I recommend a two-stage approach:

  1. Rename the archives directory to something else like "delete-soon", for example. Now, watch your site's error log for a while to make sure doing that hasn't caused archive pages to not be found. It shouldn't add any errors because if things are working properly, the references to all your archives should be caught by URL Rewriting and redirected to their new, correct, location. If renaming does cause pages to suddenly not be found, rename it back to "archives" and review all the preceding steps, because something's not working.

  2. After you're convinced that renaming the archive directory hasn't introduced any errors, delete it, and all its contents.

Now you're done.