Categories
Code Talking

Serving Content

I came across a post by someone who found something I wrote to be useful. That's cool. I'm glad someone is reading. One thing that this post reminded me of, that I didn't mention previously, is the MIME type of the document being served. In the case of…

I came across a post by someone who found something I wrote to be useful. That’s cool. I’m glad someone is reading.

One thing that this post reminded me of, that I didn’t mention previously, is the MIME type of the document being served. In the case of HTML, the longstanding tradition has been to serve the document as text/html. This is what we’ve always done, and this is what most people continue to do now.

The problem with this is that text/html is not appropriate for XHTML!

This is it in a nutshell: HTML is okay to serve as text/html. In fact, it should be served as text/html. The confusing part starts when you hit XHTML. XHTML 1.0 may be served as either text/html or the more accurate application/xhtml+xml. So if you haven’t altered your MIME types (or if your ISP hasn’t done it for you), you’re probably still okay if you’re using XHTML 1.0. But when you hit XHTML 1.1, you should then serve your content as application/xhtml+xml. No more text/html allowed.

But before you go changing your entire site, be warned that Microsoft Internet Explorer does not support the application/xhtml+xml type. In fact, it still renders your fancy new XHTML 1.0 page in plain old HTML because it’s being served with the text/html MIME type. So what will happen if you serve application/xhtml+xml to MSIE? You will be prompted to open or save the file, because it doesn’t know how to handle that stream of data. If you choose to open the file, it will display pretty much as intended. But it does create a nuisance for your readers.

Mark Pilgrim does a heck of a lot better job explaining than I ever could, and in fact supplies the code that you can use to dynamically serve your data to different browsers. I chose the PHP code and plugged it into my template that is pulled into the top of every page:

<?php
if ( stristr($_SERVER[“HTTP_ACCEPT”],”application/xhtml+xml”) ) {
header(“Content-type: application/xhtml+xml”);
}
else {
header(“Content-type: text/html”);
}
?>

This will check the browser to see if it accepts application/xhtml+xml, and if it does, that’s how it will set the content-type field. If the browser doesn’t accept this type, then it serves it up as plain old text/html. If you use this code, make sure that your page is being processed as PHP – otherwise it won’t do much good.

One reply on “Serving Content”

Thanks for the PHP code!
I’ve been looking for a solution to “deal” with Internet Explorer.
Google is my friend. 🙂

(Note that my site, the one I filled in, isn’t XML or even XHTML yet, I’m working on converting it now, heh.)

Comments are closed.