Serving Your XHTML with the Right MIME Type

As long as I remember, I make great efforts to do everything right. Good thing I consider the right way to be anyway.

Now I feel that the time has come to get answers to very important questions once and for all.

Everyone who swore on XHTML would sooner or later stumble upon a quote: โ€œIf you don't serve the right MIME type, the document will be interpreted as plain HTMLโ€

Tell what? I created a perfectly healthy XHTML document, following all standards, etc. What have I done wrong? What did I miss?

As far as I understand, this is a server thing for the most part, so of course I investigated it too, and it would seem that the header of the PHP () function is the answer to the problem.

Yes, then all is well. Well no, this is actually not the case, because no matter how much I search on the network, I just canโ€™t find consistent information on how to solve the problem, and when I find something remotely relevant, it's all about browser compatibility and the like.

Let it be said as clearly as possible.

I donโ€™t care about the computing power of the browser. (not at the moment)

All I really need is to get an epic XML error message if I make a mistake and, of course, of course, knowing how I do it.

In short, I want to part with the SGML method and embrace the XML method, and I want, even without the slightest doubt, to say that this document is valid XML / XHTML and is interpreted as such.

My thought is that I can simply require that the XHTML document in question, via a PHP script, send it with the appropriate MIME type, but how this is actually done is still a mystery due to conflicting information about net.

I hope someone can provide the answer I'm looking for, preferably with links to relevant information for backing it up. If you can do this for me, I will be forever grateful.

Sincerely.

Edit: I cannot say that I understand why and how, but at least I found a way to make it act as it should, just adding: on top of a regular xhtml document, of course, changing the file type to PHP so that actually run his script.

I am absolutely sure that this is not the end of the story, but so far I am happy.

+4
source share
2 answers

If you click on the header /xhtml+xml , IE will no longer interpret your pages. This is why no one really wants to do it right. (And the very reason I do it on another site.)

However, let your web server handle the sending of the correct MIME type. You can usually let mod_negotiation handle this. However, this requires two versions of each document:

 index.en.html index.en.xhtml 

Then, if url / resource /index requested, it will automatically determine the appropriate version of the document and send it with the correct media type. However, in reality, he does not understand the variance of the type of serialization formats and cannot establish priority. And to withstand two files with the same content is not very clear for a start.

Therefore, a simpler approach would be to use mod_rewrite to handle MIME type switching:

 RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml RewriteRule .+\.xhtml$ - [T=text/html] 

This will send a configured header for all .xhtml documents, but will override this if the browser does not specify XHTML support. This is not completely complete, because in order to avoid problems with the proxy server, you also need to set the Vary header: if you manually conduct any negotiations on the content. This requires mod_header:

 RewriteRule .+\.xhtml$ - [E=VARY_XHTML:1] Header append Vary "Accept" env=VARY_XHTML 

You can do the same with the PHP script shell, but then you lose all the benefits of having the server handle it. In any case, this is quite a lot of effort, so hardly anyone really does. But if you really need XML parsing errors, this might be a semi-implemented option.

+5
source

To get a browser parser for parsing XHTML using XML parsing, it must be sent with an XML Mime Type

HTML5 defines this as:

The term XML MIME type is used to refer to the MIME type text / xml, application / xml and any MIME type whose subtype ends with the four characters "+ xml". [RFC3023]

The most common type of mime is application/xhtml+xml , but it is far from the only possible one.

+2
source

All Articles