Serve it Up!
For the fourth in my series of MT How-To articles, I’ll focus on serving your blog as XHTML 1.1, with Content-Type of application/xhtml+xml
. Why would you want to do such a thing? Well, my reason is that I want to be able to serve embedded MathML content. You may have a similar practical reason. Or you may just enjoy following the W3C’s recommendations. I’ll assume, for the purposes of this article, that you’re interested in MathML or SVG content because that is — to my mind — the most compelling current application.
Before we plunge in, it would be wise to make sure your pages are valid XHTML 1.1 (and I mean all your pages — your individual archive pages, your Comment-Listing popup, your Search Results page, … — validate them all!). And you should follow the advice in my previous articles (I, II) about bullet-proofing your content, so that what’s valid today stays valid tomorrow.
So, where to start? We’re going to use mod_rewrite
to set the MIME-type of your static pages. Some people use PHP for this, but mod_rewrite
seems more reasonable, as you can set the MIME-type of anything from an entire web site to a single file by editing one configuration file (either .htaccess
, or your server configuation file; the latter yields higher performance, but requires restarting the server everytime you make a change). And we’re going to use the USER_AGENT
string to decide what MIME-type to send. Some people use the ACCEPT
header, but I’ve found that some browser which say they can handle application/xhtml+xml
will actually choke if you send them MathML with that Content-Type. So I want to choose explicitly which browsers to send application/xhtml+xml
Without further ado, here are the mod_rewrite rules
RewriteEngine On RewriteBase /~distler/blog/ RewriteRule ^$ index.shtml RewriteCond %{HTTP_USER_AGENT} Gecko|W3C.*Validator|MSIE.*MathPlayer RewriteRule \.html$|\.shtml$ - [T=application/xhtml+xml] RewriteCond %{HTTP_USER_AGENT} Chimera|Camino|KHTML RewriteRule \.html$|\.shtml$ - [T=text/html] RewriteCond %{HTTP_USER_AGENT} Camino.*MathML-Enabled RewriteRule \.html$|\.shtml - [T=application/xhtml+xml]
The RewriteBase
is the URL of your blog (everything after the hostname). All browsers with “Gecko” in the USER_AGENT
, except Safari and Camino get sent application/xhtml+xml
. So do the W3C Validators. Everybody else gets text/html
.
That takes care of all our static pages. For CGI-generated pages (produced by mt-comments.cgi
and mt-search.cgi
), we need to be a bit cleverer. The trick we’ll use is to set an environment variable.
First, we’ll ever-so-gently patch lib/MT/App.pm
.
--- lib/MT/App.pm.orig Tue Mar 25 11:09:03 2003 +++ lib/MT/App.pm Tue Mar 25 11:10:38 2003 @@ -52,6 +52,9 @@ sub send_http_header { my $app = shift; my($type) = @_; + if ($ENV{'HTTP_CONTENT_TYPE'}){ + $type= $ENV{'HTTP_CONTENT_TYPE'}; + } $type ||= 'text/html'; if (my $charset = $app->{charset}) { $type .= "; charset=$charset"
Then we’ll use mod_rewrite
again to set the environment variable (n. b., the name of the environment variable was specially chosen for compatibility with suEXEC. Thanks to Jacob Childress for pointing out the difficulty with an earlier version of this hint.)
RewriteEngine On RewriteBase /cgi-bin/MT-2.5 RewriteCond %{HTTP_USER_AGENT} Gecko|W3C.*Validator|MSIE.*MathPlayer RewriteRule ^mt-comments|^mt-search - [E=HTTP_CONTENT_TYPE:application/xhtml+xml] RewriteCond %{HTTP_USER_AGENT} Chimera|Camino|KHTML RewriteRule ^mt-comments|^mt-search - [E=HTTP_CONTENT_TYPE:text/html] RewriteCond %{HTTP_USER_AGENT} Camino.*MathML-Enabled RewriteRule ^mt-comments|mt-search - [E=HTTP_CONTENT_TYPE:application/xhtml+xml]
Now those CGI-generated pages will be sent out with the right Content-Type, too. That’s very handy, especially if you’re going to give people the option of leaving comments using itexToMML. Now they’ll actually be able to preview them!
One drawback of this approach comes if you have multiple blogs sharing a common MovableType installation, and you want to send some as application/xhtml+xml
and some as text/html
. There’s no problem with the static pages, but the /cgi-bin/
directory is shared between the different blogs. The most straightforward approach is to create a hard-link to mt-comment.cgi
and mt-search.cgi
in the user’s blog directory, and then use the mod_rewrite
rules there. If you do that, you need to set the value of the $MT_DIR
variable in those two cgi scripts “by hand”. Something like
$MT_DIR = '/my/mt/directory/';
(Thanks, again, to Jacob Childress, who’s implemented this particular solution on his site.)
Yuan-Chung Cheng has a different approach to setting the Content-Type in the context of a shared MovableType installation. He also has some cool things to say about using UTF-8 in this context. If you’re using UTF-8, you probably want to set
NoHTMLEntities 1
in mt.cfg
. But, if you do, it will totally mess with any HTML entities in your Comment-editing form on your Comment Preview page. Yuan-Chung has figured out the solution, which he explains in more detail here. Definitely not for the faint of heart, but if you gotta have Traditional Chinese and MathML in the same blog, this is the only way to go.
If you’ve come this far, you’ll notice that the rememberMe
javascript on your Comment-entry form has just broken. There are three problems.
For historical reasons, the javascript code supplied in MovableType is wrapped in an HTML comment
When served<script type="text/javascript" > <!-- ... //--> </script>
application/xhtml+xml
, Mozilla will studiously ignore the contents of HTML comments. You need to uncomment the script to make it work. This holds for all the javascript on your blog.- The script itself needs to be updated for XHTML compatibility. Look at the javascript in my Comment-Listing Template to see how to do it right.
- Even after all of this, you’ll need a recent version of Mozilla for cookies to work with
application/xhtml+xml
.
Well, that’s it. You should be up and running with application/xhtml+xml
! Down the road, you may have to adjust your mod_rewrite
rules, as new browsers become MathML-capable (MathML support in Camino is coming soon). But, for now, these’ll do.
Update (6/7/2003): Tweaked mod_rewrite
rules to support OmniWeb 4.5
Update (8/31/2003): Set the MIME type sent to the W3C Validators to application/xhtml+xml
as well. That way, they see the page as it “should” be seen.
Update (10/20/2003): Send application/xhtml+xml
to Dave Haas’s MathML-enabled builds of Camino.
Update (2/16/2004): Add support for Mathplayer 2.0.
Re: Serve it Up!
Very helpful indeed. I actually threw out the
rememberMe
Javascript code when I made the move to XHTML, but maybe I’ll work it back in to my site.I had forgotten to mention it when discussing this topic with you before, but of course you’ll also need to create a hard link to mt-tb.cgi if your situation is like mine.