application/xhtml+xml is the official MIME type for serving XHTML pages with. Although it is possible to serve some flavours of XHTML with different MIME types it's generally recommended that application/xhtml+xml is used.

The possible MIME types for XHTML documents are:

  • application/xhtml+xml - the recommended type in the specification
  • application/xml - as XHTML is a XML implementation then this is a legal way of serving pages. The specification warns that clients may not always process the served document as XHTML though.
  • text/xml - RFC 3023 states that text/xml served documents should be human-readable, as opposed to machine oriented application/xml. There's no hard and fast rules though. This is also a legal way of serving XHTML pages.
  • text/html - this is the official MIME type for serving HTML and may be used for XHTML 1.0. It's usage is less that optimal though as pages served with this MIME type won't be processed as XML documents. You then lose many of the benefits of XML such as a demand for well-formed pages. It is also useless for serving any pages other than pages that follow the HTML compatibility guidelines. It may not be used for XHTML 1.1 or later.
  • So far, so good. application/xhtml+xml seems to fit all of our requirements, with no detriment to the user.

    Enter Internet Explorer.

    Internet Explorer (the world's most widely used web browser at the time of writing) is yet to be able to make head or tail of application/xhtml+xml. If you serve a page as application/xhtml+xml IE won't understand what to do and will offer to save the page rather than render it. For pages conforming to the HTML compatibility guidelines then we have a choice. We can either serve the page as text/html to all users and miss out on the benefits specified above or we can use some Apache rewrite rules to serve the page in the appropriate MIME type to specific user agents. This of course assumes that you're using Apache. To do it, find your site's .htaccess file and add the following text:

    AddType text/html;charset=utf-8 .html
    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} Opera [OR]
    RewriteCond %{HTTP_USER_AGENT} Gecko [OR]
    RewriteCond %{HTTP_USER_AGENT} Konqueror [OR]
    RewriteCond %{HTTP_USER_AGENT} KHTML
    RewriteRule \.html$ - [T=application/xhtml+xml;charset=utf-8]

    changing the charset as appropriate of course. Yes, this is a kludge, but it works. Sooner or (probably) later Microsoft will release a version of IE that supports application/xhtml+xml but until that time this gives your users the best of both worlds.


    vruba adds:

    PHP is my preferred kludging language for mime types. I use:

    <?php if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE"))
        $xhtmlmime = "text/html";
        else $xhtmlmime = "application/xhtml+xml"; ?>

    And then:

    <meta http-equiv="content-type" content="<?php echo($xhtmlmime); ?>; charset=UTF-8" />

    No big difference, but I like that because it singles out the problem -- IE -- instead of selecting the non-pathological browsers.


    References:

    • "XHTML Media Types" at w3.org - http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/
    • "RFC3023 - XML Media Types" at faqs.org - http://www.faqs.org/rfcs/rfc3023.html
    • "application/xhtml+xml" at exclipy! - http://www.exclipy.com/blog/0005.html

    Log in or register to write something here or to contact authors.