fettig.netBlog2004 → Hiding File Extensions in Movable Type

Hiding File Extensions in Movable Type

Brad Choate has a good post on how he uses Moveable Type to generate documents with a file extension, but then removes that file extension from his URls. I’ve tried to accomplish the same thing on fettig.net. Most of what I do is the same as Brad, but there are a few differences in my technique, which I’ll describe here.

I use the following mod_rewrite rule to send a client-side redirect response if a visitor tries to access a URI with a .html, .php, or .cgi extension:

# file.* -> file
RewriteCond %{THE_REQUEST} GET\ /.*\.(html|cgi|php)\  [NC]
RewriteRule ^(.*)(\.[A-Za-z0-9]*)$ http://%{HTTP_HOST}/$1 [L,R]

This doesn’t address the issue of the Content-Location header that Brad is worried about, but it does make sure a visitors see an extension-less URI in their location bars, which they will then use for linking or bookmarking purposes.
For example, try clicking on this link:
http://www.fettig.net/2004/03/switching_to_emacs.html.

Like brad, I’ve also had to go through all all my MT templates to strip the extension. The only differenct is that I encapsulated the regular expression logic in a little plugin called “strip_extension.pl”. Here’s the code:

use strict;
use MT::Template::Context;

MT::Template::Context->add_global_filter(strip_ext => sub {
        my $s = shift;
        $s =~ s/\.[A-Za-z0-9]+\Z//g;
        return $s;
});

Then in my templates, I replace all instances of <$MTEntryLink$> with <$MTEntryLink strip_ext="1"$>.

Comments are closed.