<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>thought-after &#187; Content Negotiation</title>
	<atom:link href="http://www.thought-after.com/category/content-negotiation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thought-after.com</link>
	<description>Geeky stuff by Lafinboy</description>
	<lastBuildDate>Mon, 24 Oct 2011 00:05:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Serving the correct MIME type</title>
		<link>http://www.thought-after.com/2006/03/serving-the-correct-mime-type/</link>
		<comments>http://www.thought-after.com/2006/03/serving-the-correct-mime-type/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 22:49:18 +0000</pubDate>
		<dc:creator>Lafinboy</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[Content Negotiation]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[MIME Types]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://www.thought-after.com/2006/03/16/serving-the-correct-mime-type/</guid>
		<description><![CDATA[Working with XHTML is all very commendable, but if the default (read wrong) MIME type is sent to the UA then you are still only sending HTML documents out to &#8230; <a class="more" href="http://www.thought-after.com/2006/03/serving-the-correct-mime-type/">Continue reading</a>]]></description>
			<content:encoded><![CDATA[<p>Working with XHTML is all very commendable, but if the default (read <em>wrong</em>) MIME type is sent to the UA then you are still only sending HTML documents out to browserland. If this is the case you may as well work with the HTML doctype. So how do you send the correct MIME type for XHTML?</p>
<p>There are various methods depending on your situation. One can configure the server to send the correct MIME type based on file type, use httpd or htaccess files, or force the correct MIME type through the header of the page using your favourite server side code (ASP, PHP, .NET, et al). There&#039;s just one problem though &#8211; Internet Explorer currently doesn&#039;t support documents served with the</p>
<p><code class="inline">application/xhtml+xml</code> MIME type. This is where we need to perform a negotiation between the server and browser about the media types the browser can accept, and which one it prefers.<span id="more-11"></span>The basics of content negotiation are:</p>
<ol>
<li>Server says hello to the browser, and ask if it can send it a page</li>
<li>Browser says hi back, and tells the server what media types it can accept, and of those which is it&#039;s preferred media type</li>
<li>Server get&#039;s the document in the browsers preferred format and send it out</li>
<li>Browser displays our correctly formatted page</li>
</ol>
<p>So by evaluating the media types and relative weighting of each type we can determine whether the browser shoud be sent files as <code class="inline">text/html</code> or <code class="inline">application/xhtml+xml</code>.</p>
<p>A sample PHP content negotiation script is shown below:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$charset</span> <span class="sy0">=</span> <span class="st0">&quot;utf-8&quot;</span><span class="sy0">;</span><br />
<span class="re0">$mime</span> <span class="sy0">=</span> <span class="st0">&quot;text/html&quot;</span><span class="sy0">;</span><br />
<span class="kw2">function</span> xml2html<span class="br0">&#40;</span><span class="re0">$buffer</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="kw1">return</span> <span class="br0">&#40;</span><a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&quot;!\s*/&gt;!&quot;</span><span class="sy0">,</span> <span class="st0">&quot;&gt;&quot;</span><span class="sy0">,</span> <span class="re0">$buffer</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/stristr"><span class="kw3">stristr</span></a><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&quot;HTTP_ACCEPT&quot;</span><span class="br0">&#93;</span><span class="sy0">,</span><span class="st0">&quot;application/xhtml+xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&quot;/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i&quot;</span><span class="sy0">,</span> <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&quot;HTTP_ACCEPT&quot;</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re0">$matches</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$xhtml_q</span> <span class="sy0">=</span> <span class="re0">$matches</span><span class="br0">&#91;</span>1<span class="br0">&#93;</span><span class="sy0">;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&quot;/text\/html;q=q=([01]|0\.\d{1,3}|1\.0)/i&quot;</span><span class="sy0">,</span> <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st0">&quot;HTTP_ACCEPT&quot;</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re0">$matches</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$html_q</span> <span class="sy0">=</span> <span class="re0">$matches</span><span class="br0">&#91;</span>1<span class="br0">&#93;</span><span class="sy0">;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span>float<span class="br0">&#41;</span><span class="re0">$xhtml_q</span> <span class="sy0">&gt;=</span> <span class="br0">&#40;</span>float<span class="br0">&#41;</span><span class="re0">$html_q</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$mime</span> <span class="sy0">=</span> <span class="st0">&quot;application/xhtml+xml&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
<span class="re0">$mime</span> <span class="sy0">=</span> <span class="st0">&quot;application/xhtml+xml&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$mime</span> <span class="sy0">==</span> <span class="st0">&quot;application/xhtml+xml&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$prolog_type</span> <span class="sy0">=</span> <span class="st0">&quot;&lt; ?xml version=<span class="es1">\&quot;</span>1.0<span class="es1">\&quot;</span> encoding=<span class="es1">\&quot;</span><span class="es4">$charset</span><span class="es1">\&quot;</span> ?&gt;</span>\n&lt; !DOCTYPE html PUBLIC \&quot;-//W3C//DTD XHTML 1.0 Strict//EN\&quot; \&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\&quot;&gt;\n\n&quot;;<br />
$meta_content = &quot;&lt;meta http-equiv=&quot;\&quot; content=&quot;\&quot; /&gt;\n&quot;;<br />
} else {<br />
ob_start(&quot;xml2html&quot;);<br />
$prolog_type = &quot;&lt; !DOCTYPE html PUBLIC \&quot;-//W3C//DTD HTML 4.01 Strict//EN\&quot; \&quot;http://www.w3.org/TR/html4/strict.dtd\&quot;&gt;\n\n&quot;;<br />
$meta_content = &quot;&lt;meta http-equiv=&quot;\&quot; content=&quot;\&quot; /&gt;\n&quot;;<br />
}<br />
header(&quot;Content-Type: $mime;charset=$charset&quot;);<br />
header(&quot;Vary: Accept&quot;);<br />
print $prolog_type;<br />
?&gt;</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thought-after.com/2006/03/serving-the-correct-mime-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

