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.
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.