HTML and CSS Reference
In-Depth Information
The HTTP specification defines the mechanism for serving different versions of the same resource [1]. Document
types, document languages, and image types are some examples [52]. The preferred and acceptable document
format(s)—in our case, the preference between HTML and XHTML files—can be defined in the HTTP header, as
shown in Listing 4-4.
Listing 4-4. Content Negotiation in the HTTP Header
Accept: text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8
Using the previous example, the browser can specify that HTML and XHTML are preferred to XML. The
“relative quality parameter” ( q ) and its value ( qvalue ) are considered as follows. All items without a specified
preference value get the default value 1 (in this case text/html and application/xhtml+xml ). The specified value for
application/xml is 0.9 , and all the other formats 0.8 . The precedence values ordered in descending order reveal the
actual precedence, in other words, 1 for text/html and application/xhtml+xml , 0.9 for application/xml , and 0.8
for any other content types.
On Apache servers, the directive shown in Listing 4-5 should be added to your .htaccess (or httpd.conf ) file to
set the HTTP headers required for the correct MIME type.
Listing 4-5. Preference Between text/html and application/xhtml+xml
Options +Multiviews
AddType application/xhtml+xml;qs=0.8
AddType text/html;qs=0.9
The “quality of source” parameter (qs), set to 0.8 in our example, determines whether the AddType directive
applies the specified MIME type. Since the qs value for application/xhtml+xml is smaller than that of text/html ,
application/xhtml+xml will be used by compliant browsers only; otherwise, the preferred version will be the MIME
type text/html .
Content negotiation can also be implemented in server-side scripting languages such as PHP (Listing 4-6), ASP
(Listing 4-7), and C# (Listing 4-8).
Listing 4-6. Content Negotiation in PHP
$accept = $_SERVER["HTTP_ACCEPT"];
$ua = $_SERVER["HTTP_USER_AGENT"];
if (isset($accept) && isset($ua)) {
if (stristr($accept, "application/xhtml+xml") || stristr($ua, "W3C_Validator")) {
header("Content-Type: application/xhtml+xml");
}
}
Listing 4-7. Content Negotiation in ASP
Dim strAccept, strUA
strAccept = Request.ServerVariables("HTTP_ACCEPT").Item
strUA = Request.ServerVariables("HTTP_USER_AGENT").Item
If InStr(1, strAccept, "application/xhtml+xml") > 0 Or InStr(1, strUA, "W3C_Validator") > 0
Then Response.ContentType = "application/xhtml+xml"
End If
 
Search WWH ::




Custom Search