Baby steps

Mark Pilgrim has updated his site based on recommendations from Ian Hickson on building a perfect Web log. Following some of their lead, I’ve added the first bit of browser checking to my site using Apache’s mod_rewrite. The module has always scared me, but using what amounts to an identity rewrite I’ve managed to dynamically change the MIME type returned for my weblog pages. Here’s how it works.

RewriteEngine on

This line turns on the rewriting engine for the current directory and all subdirectories below it.

RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[5-9].* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Amaya/[5-9].*

These two lines define the conditions where the following rule will apply. They look at the HTTP_USER_AGENT variable (i.e. the User-Agent: field in the HTTP request) and check that it begins with Mozilla/ or Amaya/ followed by a major revision that is known or presumed to support the correct MIME type for XHTML files. (I’m working under the assumption that if it’s supported in an earlier version it will continue to be supported in a later one.)

RewriteRule ^(.*)\.html$ - [T=application/xhtml+xml]

Finally, I add the rule itself: any file that has the extension .html gets mapped to exactly the same name. By itself that’s useless and just wastes cycles on each request… except for the T= field that resets the MIME type, which was the original goal.

Easy peasy.

Right now, all of these lines are in a .htaccess file in the weblog directory. If/when I update the rest of the site to a version of XHTML (most of it has been, but I’m not confident enough to switch completely yet), these rules will migrate to my global httpd.conf.

Published by

2 thoughts on “Baby steps

  1. Which is almost entirely the opposite of Ian Hickson’s browser-checking demo at . He also does a little more, such as configuring a charset; I think my file is a little closer to Mark’s description. Both work, and both have their advantages.

    I have no idea what the HTML and XHTML files are showing wrt mod_rewrite; they’re probably just test files that were handy.

  2. Those darned too-smart people. Forget browser matching, sniffing the Accepts: header is the way to go (although sniffing is still bogus in general). I actually submitted a patch to the Mozilla tree (the one tiny bit of contribution I’ve made to that project) to enable video/x-mng in the header, so I can’t even claim to have been unaware of it.

    http://petesguide.com/blog/2002_11_01_archive.html#Item85709376

    On the other hand, I already have MultiViews turned on, as Pete’s done; all I need to do to get back to a completely extensionless site is figure out how to get MovableType to not include extensions in its URLs. (A lot of the .html files on my site are actually XHTML, including this weblog, otherwise I’d use the standard extension mapping Pete sets up here.)

    http://petesguide.com/blog/2002_11_01_archive.html#Item85709451

Comments are closed.