Warning: include(/ssi/prev.maha) [function.include]: failed to open stream: No such file or directory in /homepages/12/d123634311/htdocs/asolis/article/xhtmlMime.maha on line 1

Warning: include() [function.include]: Failed opening '/ssi/prev.maha' for inclusion (include_path='.:/usr/lib/php5') in /homepages/12/d123634311/htdocs/asolis/article/xhtmlMime.maha on line 1

The XHTML MIME Type

If you know much about web development and XHTML, you've probably heard about serving XHTML with the correct MIME type. If you haven't heard of the application/xhtml+xml MIME type, well, now you have. HTML <= 4.01 must be served as text/html. For more information on the text/html MIME type see the RFC page.

Now for why XHTML should be served with the application/xhtml+xml. Well, first there's the fact that the W3C specifically states that you should. This is because serving XHTML as HTML is considered to be tag soup. The W3C specification states that you should send XHTML documents as application/xhtml+xml so the web page will only display in the User Agent if it is a well-formed XHTML document. This will help the cross-compatibility between user agents and help in the development of the web.

Now for how to serve XHTML documents as XHTML. You can save your web pages with the .xhtml or .xht extensions. If you have a fair amount of control over your server/web hosting you should be able to use .htaccess to do this. We'll start with this approach.


RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]

What this does is for "less capable browsers"—Internet Explorer and older browsers—serves the XHTML as tag soup. If you don't care about the users of "bad browsers" you can always just use this code:

AddType application/xhtml+xml html

If you want to have more power over how you serve it, you can always use PHP or another language.
For PHP:


<?php
if
  ( stristr($_SERVER[\"HTTP_ACCEPT\"],\"application/xhtml+xml\")
  OR strstr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')
  OR strstr($_SERVER['HTTP_USER_AGENT'], 'WDG_SiteValidator')) { header(\"Content-type: application/xhtml+xml\");
}
else {
header(\"Content-type: text/html\");
}
?>

This is an example of the PHP code used to send your web page as XHTML to the W3C validator and the WDG Site validator. There are better ways to do this using PHP that change the way you serve your stylesheets, doctype, and MIME type too. See Keystone Websites' article and scroll about half way down the web page for a more complex PHP technique and read their article, too.

XML.com offers a Python technique and also some other good information on how to serve XHTML using the correct MIME type.

Happy XHTML'ing!

Other Resources:
Warning: include(/ssi/after.maha) [function.include]: failed to open stream: No such file or directory in /homepages/12/d123634311/htdocs/asolis/article/xhtmlMime.maha on line 99

Warning: include() [function.include]: Failed opening '/ssi/after.maha' for inclusion (include_path='.:/usr/lib/php5') in /homepages/12/d123634311/htdocs/asolis/article/xhtmlMime.maha on line 99