Post Archive

› October 25, 2003

Serving up XHTML with the correct MIME type

  • Reported by Russ

"For most websites, authoring in HTML 4.01 is perfectly sufficient. Most of the features available in XHTML are available in good old HTML. However, some sites may wish to take advantage of the extensibility of XML, so delivering in XHTML with the correct MIME may be important."

Simon Jessey of Keystone Websites has developed a technique that takes advantage of the PHP server-side scripting language. Web pages can be served as either XHTML with application/xhtml+xml, or HTML with text/html. An excellent article!

Comments

1. October 25, 2003 10:37 AM

Quote this comment

Mark Wubben Posted…

Credit where credit is due: Send application/xhtml+xml.

2. October 25, 2003 11:07 AM

Quote this comment

Simon Jessey Posted…

Mark Wubben is correct. Anne van Kesteren gave me considerable assistance when I began using this technique, and he is credited at the bottom of the article.

Unfortunately, it seems that the technique is flawed. The HTML 4.01 that is served up is invalid because it isn't supposed to have any closed empty tags, like the META and LINK elements, for example.

3. October 25, 2003 12:37 PM

Quote this comment

Simon Jessey Posted…

I've written a function that fixes the problem mentioned in the previous comment. The HTML now validates.

4. October 25, 2003 01:13 PM

Quote this comment

Mark Wubben Posted…

Something I actually forgot to mention, cool article Simon! I've been playing with this on my own site (it launched last monday), where I'm sending valid XHTML1.1 to XML browsers and valid HTML 4.01 to HTML browsers.

Wow, I never knew you could "fix code" with ob_start(). When I have some time I will use that in my site's backend.

Anyway, if I sounded harsh in my first comment, I apologize for that.

5. October 25, 2003 04:00 PM

Quote this comment

Simon Willison Posted…

Just a small note - you don't need to use ereg_replace for fixing the closed tags as it doesn't involve a regular expression - str_replace would provide better performance.

I wrote something similar to this for www.bcss.info last year, but never really did anything with it. The site defaults to serving HTML 4.01 but you can append ?xhtml=1 to any URL and it will serve the page up as valid XHTML (with the correct mime type if the browser supports it) instead.

6. October 25, 2003 09:24 PM

Quote this comment

Simon Jessey Posted…

Thanks for the tip, Simon. I'm not much of a programmer and I'm very new to PHP. I must confess that I used the very first function I stumbled across that did the trick! I'd like to continue refining the process, so I'll probably replace it with str_replace, as you suggest.