Serving your XHTML with the correct MIME type
As long as I can remember, I've make a great effort to do things the proper way. Well, what I consider the proper way to be anyway.
Now I feel that it is time to get some very important questions answered once and for all.
All who swear to XHTML will, sooner or later, stumble upon the quote: "unless you serve the right MIME type, you document will be interpreted as regular HTML"
Say what? I've created a perfectly sound XHTML document, following all the standards and stuff. What have I done wrong? What have I missed?
As far as I understand it, it's a server thing for the most part, so of course I've investigated that too, and it would seem that the PHP function header() is the answer to the problem.
Yay, then all is good. Well no, in fact it's not, because no matter how much I search the net, I am simply unable to find consistent information about how to go about the problem and when I do find something remotely relevant, it's all about browser comparability and such.
Let it be said as clearly as possible.
I so not care about browser computability. (not at this point anyway)
All I really want is receive the XML type epic fail messa开发者_如何学JAVAge if I've made a mistake and of course of course the knowledge about how I actually make this happen.
In short, I want to part with the SGML way and embrace the XML way, and I want to be able, without even the tiniest bit of doubt, to say that this document is valid XML/XHTML and is interpreted as such.
My thought is that I can simply require the XHTML document in question, via a PHP script, and send it of with a proper MIME type, but how it is actually done is still a mystery due to conflicting information on the net.
I do hope that someone will be able to supply the answer I'm looking for, preferably with links to the relevant information to back it up. If you can do this for me, I will be forever grateful.
Best regards.
Edit: I can't say that I understand why or how, but at least I found a way to make it act as it should, simply by adding: to the top of a regular xhtml document, of course changing the filetype to PHP to actually make it run the script.
I'm quite sure that this is not the end of the story, but for now I'm happy.
If you force the /xhtml+xml
header, then IE won't interpret your pages anymore. Which is why nobody actually bothers to do it correctly. (And the very reason I do it on another site.)
It's however possible to let your webserver handle the sending of the correct MIME type. Normally you could let mod_negotiation handle that. However that requires having two versions of each document:
index.en.html
index.en.xhtml
Then if a url/resource /index
is requested, it will automatically determine the appropriate document version, and send it with the right media type. However, it doesn't really understand the type variance of the serialization formats, nor can you set a precedence. And holding out two files with the same content is not overly senseible to begin with.
Therefore an easier approach would be to use mod_rewrite to handle the MIME type switching:
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteRule .+\.xhtml$ - [T=text/html]
This will send the configured header for all .xhtml documents, but override that if the browser does not indicate XHTML support. That's not quite complete, because to avoid proxy issues you also need to set the Vary: header if you manually do any kind of content negotation. This requires mod_header:
RewriteRule .+\.xhtml$ - [E=VARY_XHTML:1]
Header append Vary "Accept" env=VARY_XHTML
You can do the same with a PHP script wrapper, but then you lose all the benefits of having the server handle it. Either way, it's quite a bit of effort, which is why hardly anyone really does it. But if you really want the XML parsing errors, this might be a semi-workable option.
To get a browser parser to parse XHTML with an XML parser, it must be served with an XML Mime Type
HTML5 defines this as:
The term XML MIME type is used to refer to the MIME types text/xml, application/xml, and any MIME type whose subtype ends with the four characters "+xml". [RFC3023]
The most common such mime type is application/xhtml+xml
, but it is far from being the only possible one.
精彩评论