Emulating ContentTypePriority in Apache
I found what I believe is an undocumented trick in Apache 2.0. You can control the quality values Apache uses in content negotation with the <ForceType>
directive.
When serving multiple content types from a single URI using MultiViews
, Apache uses some oddly complex method of determining which file type to send. You can override this by creating .var
files but that sucks sometimes. I really wanted something like the <LanguagePriority>
that let’s you specify which types should have priority. The following achieves that same result using the <ForceType>
directive.
<FilesMatch ".*\.html$"> ForceType text/html;charset=utf-8;qs=0.4 </FilesMatch> <FilesMatch ".*\.xml$"> ForceType application/xml;charset=utf-8;qs=0.3 </FilesMatch>
This tells Apache, should you come across two files that differ only in extension, and everything else being equal with the client (e.g. the client didn’t specify a q
value that would weigh one of these types heavier), that the server should send back the one with the .html
extension.
UPDATE: June 25, 2004 3:32 AM
It looks like this is another area where IE’s standards support is shaky. While the qs value specified here gets picked up by Apache and used to determine which representation to pick, it looks like the Content-Type response header also contains the qs value. This results in IE thinking the qs value is part of the charset or something. I’ve been able to break it two separate ways.
- When the qs is before the charset, IE ignores the charset and loads the page using its default charset.
- When the qs is after the charset, IE gives me an error page saying that the “System does not support the specified encoding.”
Chalk up another one for Microsoft!
UPDATE: June 26, 2004 11:29 PM
Found some more discussion on this topic:
- Re: 'qs' parameter in Content-Type header
- Some discussion on the IETF HTTP WG mailing list (June. 2002)
- Specifying a MIME Type / Content Negotiation
- Doh! Might be able to solve same problem with AddType directive.
UPDATE: July 05, 2004 8:36 PM
The AddType directive seems to be the easiest method of accomplishing this. Right now, my .htaccess file has the following:
AddType text/html;charset=utf-8 .html AddType application/xml;charset=utf-8;qs=0.9 .xml AddType application/atom+xml;charset=utf-8;qs=0.8 .atom
If no qs value is specified, a default of 1.0 is assumed. The result of all this is that .html files will be served first, followed by .xml files, followed by .atom files.
There does seem to be an issue with Internet Explorer’s handling of XML files containing <?xml-stylesheet?> processing instructions when served with a qs value.