Future-proof your URIs

September 14, 2003

I've taken some steps to future-proof the archives of this site. The idea is that even as the underlying technology changes, the URI of the entries should not — links should work indefinitely. Out of the box, Movable Type does a bad job of future-proofing your archives as it simply puts the entry in a file (you give it the extension) named by the internal ID that MT uses (e.g., 000164.php). The problem with this is that it will break to all hell if and when you export/import your entries to a new MT setup on a different server — none of the links will point to the correct file as the internal ID of the file will have changed.

I wanted to setup a hierarchical structure for the archives that would include the title of the post and keep the filename out of the link. Now, instead of "/archives/000200.php" for this entry, I have "/archives/2003/09/15/futureproof_your_uris/." You'll notice that the filename has been removed and the URI now points to a directory. Under this directory resides an index.php file for that entry. In the future, if the technology changes (say I start using .xml instead of .php), I can make the needed modifications without breaking the link. If this interests you at all (it should!) and you use MT (you should!), then you can make the same changes to your site by following the instructions below.

Step One

Download and install the Regex plugin from Brad Choate.

Step Two

From MT's control panel, go into "Weblog Config." Choose the "Archiving" tab. Put the following in the "Archive File Template" field of the Individual "Archive Type":

<$MTArchiveDate format="%Y/%m/%d"$>/<$MTEntryTitle dirify="1"$>/index.php

This will create the /year/month/day/title/index.php structure. You'll want to use dirify="1" here as this will remove all spaces (and any other characters that can't be used in directory names) and replace them with an underscore (some things aren't replaced with underscores but are removed instead).

Step Three

For whatever file(s) you plan on using the archive link in, you need to add the following to the top of that file's template:

<MTRegexDefine name="noExtension">s|/index\.[^/]+$|/|g</MTRegexDefine>

Whenever this is called (next step), it will remove the file part of the link (/title/index.php becomes /title/) before the URI is created, thus making the URI file-independent.

Step Four

The final step is calling the regex plugin. Wherever you use the archive link tags, you need to add the following to the tag: regex="noExtension"

For example,

<a href="<$MTEntryLink regex="noExtension" archive_type="Individual"$>">

This does what was explained in step three above.

There you have it, future-proof URIs. As things change in the future, all you'll need to do is modify/define the default file loaded for a directory. For instance, all I need now is one line in the .htaccess file: DirectoryIndex index.php.

You should follow me on Twitter here