content-negotiation.xml revision c68aa7f213d409d464eaa6b963afb28678548f4f
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<?xml version='1.0' encoding='UTF-8' ?>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<!-- $LastChangedRevision$ -->
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<!--
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Licensed to the Apache Software Foundation (ASF) under one or more
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere contributor license agreements. See the NOTICE file distributed with
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere this work for additional information regarding copyright ownership.
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere The ASF licenses this file to You under the Apache License, Version 2.0
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen (the "License"); you may not use this file except in compliance with
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen the License. You may obtain a copy of the License at
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen http://www.apache.org/licenses/LICENSE-2.0
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Unless required by applicable law or agreed to in writing, software
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen distributed under the License is distributed on an "AS IS" BASIS,
3f08db06526d6901aa08c110b5bc7dde6bc39905nd WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere See the License for the specific language governing permissions and
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere limitations under the License.
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere-->
3f08db06526d6901aa08c110b5bc7dde6bc39905nd
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<manualpage metafile="content-negotiation.xml.meta">
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<title>Content Negotiation</title>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<summary>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
fac8c35bfb158112226ab43ddf84d59daca5dc30nd <p>Apache HTTPD supports content negotiation as described in
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen the HTTP/1.1 specification. It can choose the best
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen representation of a resource based on the browser-supplied
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere preferences for media type, languages, character set and
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere encoding. It also implements a couple of features to give
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere more intelligent handling of requests from browsers that send
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere incomplete negotiation information.</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>Content negotiation is provided by the
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <module>mod_negotiation</module> module, which is compiled in
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor by default.</p>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor</summary>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor<section id="about"><title>About Content Negotiation</title>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>A resource may be available in several different
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere representations. For example, it might be available in
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere different languages or different media types, or a combination.
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere One way of selecting the most appropriate choice is to give the
fabd4bc0c499704e644a74f78cc3871436824ea0jim user an index page, and let them select. However it is often
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere possible for the server to choose automatically. This works
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere because browsers can send, as part of each request, information
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere about what representations they prefer. For example, a browser
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere could indicate that it would like to see information in French,
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere if possible, else English will do. Browsers indicate their
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor preferences by headers in the request. To request only French
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere representations, the browser would send</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<example>Accept-Language: fr</example>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>Note that this preference will only be applied when there is
fabd4bc0c499704e644a74f78cc3871436824ea0jim a choice of representations and they vary by language.</p>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
bed3c2e56e8f3328e780200466b9d009093db468sf <p>As an example of a more complex request, this browser has
bed3c2e56e8f3328e780200466b9d009093db468sf been configured to accept French and English, but prefer
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere French, and to accept various media types, preferring HTML over
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere plain text or other text types, and preferring GIF or JPEG over
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere other media types, but also allowing any other media type as a
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere last resort:</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
591f748210dc55f2972482dddc84bb6bac61d6b9noodl<example>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Accept-Language: fr; q=1.0, en; q=0.5<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Accept: text/html; q=1.0, text/*; q=0.8, image/gif; q=0.6, image/jpeg; q=0.6, image/*; q=0.5, */*; q=0.1
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor</example>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <p>httpd supports 'server driven' content negotiation, as
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere defined in the HTTP/1.1 specification. It fully supports the
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <code>Accept</code>, <code>Accept-Language</code>,
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <code>Accept-Charset</code> and<code>Accept-Encoding</code>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere request headers. httpd also supports 'transparent'
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere content negotiation, which is an experimental negotiation
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere protocol defined in RFC 2295 and RFC 2296. It does not offer
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere support for 'feature negotiation' as defined in these RFCs.</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>A <strong>resource</strong> is a conceptual entity
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere identified by a URI (RFC 2396). An HTTP server like Apache HTTP Server
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere provides access to <strong>representations</strong> of the
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere resource(s) within its namespace, with each representation in
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere the form of a sequence of bytes with a defined media type,
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere character set, encoding, etc. Each resource may be associated
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere with zero, one, or more than one representation at any given
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor time. If multiple representations are available, the resource
fabd4bc0c499704e644a74f78cc3871436824ea0jim is referred to as <strong>negotiable</strong> and each of its
fabd4bc0c499704e644a74f78cc3871436824ea0jim representations is termed a <strong>variant</strong>. The ways
4126704c4950bfd46d32ad54e3b106ac6d868a73sf in which the variants for a negotiable resource vary are called
fabd4bc0c499704e644a74f78cc3871436824ea0jim the <strong>dimensions</strong> of negotiation.</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere</section>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<section id="negotiation"><title>Negotiation in httpd</title>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <p>In order to negotiate a resource, the server needs to be
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor given information about each of the variants. This is done in
fabd4bc0c499704e644a74f78cc3871436824ea0jim one of two ways:</p>
fabd4bc0c499704e644a74f78cc3871436824ea0jim
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <ul>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <li>Using a type map (<em>i.e.</em>, a <code>*.var</code>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor file) which names the files containing the variants
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor explicitly, or</li>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <li>Using a 'MultiViews' search, where the server does an
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor implicit filename pattern match and chooses from among the
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor results.</li>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor </ul>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <section id="type-map"><title>Using a type-map file</title>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <p>A type map is a document which is associated with the handler
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor named <code>type-map</code> (or, for backwards-compatibility with
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor older httpd configurations, the <glossary>MIME-type</glossary>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <code>application/x-type-map</code>). Note that to use this
fabd4bc0c499704e644a74f78cc3871436824ea0jim feature, you must have a handler set in the configuration that
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor defines a file suffix as <code>type-map</code>; this is best done
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor with</p>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<example>AddHandler type-map .var</example>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>in the server configuration file.</p>
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere <p>Type map files should have the same name as the resource
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere which they are describing, and have an entry for each available
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere variant; these entries consist of contiguous HTTP-format header
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere lines. Entries for different variants are separated by blank
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsf lines. Blank lines are illegal within an entry. It is
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere conventional to begin a map file with an entry for the combined
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere entity as a whole (although this is not required, and if
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor present will be ignored). An example map file is shown below.
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor This file would be named <code>foo.var</code>, as it describes
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere a resource named <code>foo</code>.</p>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor<example>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor URI: foo<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor URI: foo.en.html<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor Content-type: text/html<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Content-language: en<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor URI: foo.fr.de.html<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor Content-type: text/html;charset=iso-8859-2<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor Content-language: fr, de<br />
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor</example>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor <p>Note also that a typemap file will take precedence over the
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor filename's extension, even when Multiviews is on. If the
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor variants have different source qualities, that may be indicated
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor by the "qs" parameter to the media type, as in this picture
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor (available as JPEG, GIF, or ASCII-art): </p>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor<example>
999942e8e84bcae9b439ab30a040b1b997b343c9gryzor URI: foo<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere URI: foo.jpeg<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Content-type: image/jpeg; qs=0.8<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere URI: foo.gif<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere Content-type: image/gif; qs=0.5<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere<br />
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere URI: foo.txt<br />
fac8c35bfb158112226ab43ddf84d59daca5dc30nd Content-type: text/plain; qs=0.01<br />
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen</example>
d474d8ef01ec5c2a09341cd148851ed383c3287crbowen
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh <p>qs values can vary in the range 0.000 to 1.000. Note that
ba543b319188dc1887607f6d59feddc00e38eee2humbedooh any variant with a qs value of 0.000 will never be chosen.
0d0ba3a410038e179b695446bb149cce6264e0abnd Variants with no 'qs' parameter value are given a qs factor of
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh 1.0. The qs parameter indicates the relative 'quality' of this
0d0ba3a410038e179b695446bb149cce6264e0abnd variant compared to the other available variants, independent
0d0ba3a410038e179b695446bb149cce6264e0abnd of the client's capabilities. For example, a JPEG file is
0d0ba3a410038e179b695446bb149cce6264e0abnd usually of higher source quality than an ASCII file if it is
0d0ba3a410038e179b695446bb149cce6264e0abnd attempting to represent a photograph. However, if the resource
0d0ba3a410038e179b695446bb149cce6264e0abnd being represented is an original ASCII art, then an ASCII
0d0ba3a410038e179b695446bb149cce6264e0abnd representation would have a higher source quality than a JPEG
0d0ba3a410038e179b695446bb149cce6264e0abnd representation. A qs value is therefore specific to a given
0d0ba3a410038e179b695446bb149cce6264e0abnd variant depending on the nature of the resource it
0d0ba3a410038e179b695446bb149cce6264e0abnd represents.</p>
0d0ba3a410038e179b695446bb149cce6264e0abnd
0d0ba3a410038e179b695446bb149cce6264e0abnd <p>The full list of headers recognized is available in the <a
0d0ba3a410038e179b695446bb149cce6264e0abnd href="mod/mod_negotiation.html#typemaps">mod_negotation
0d0ba3a410038e179b695446bb149cce6264e0abnd typemap</a> documentation.</p>
0d0ba3a410038e179b695446bb149cce6264e0abnd</section>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
5effc8b39fae5cd169d17f342bfc265705840014rbowen<section id="multiviews"><title>Multiviews</title>
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen
0d0ba3a410038e179b695446bb149cce6264e0abnd <p><code>MultiViews</code> is a per-directory option, meaning it
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd can be set with an <directive module="core">Options</directive>
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd directive within a <directive module="core"
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd type="section">Directory</directive>, <directive module="core"
e3659090e9bb9934465dbb6c6212ba4c512190b0jfclere type="section">Location</directive> or <directive module="core"
type="section">Files</directive> section in
<code>httpd.conf</code>, or (if <directive
module="core">AllowOverride</directive> is properly set) in
<code>.htaccess</code> files. Note that <code>Options All</code>
does not set <code>MultiViews</code>; you have to ask for it by
name.</p>
<p>The effect of <code>MultiViews</code> is as follows: if the
server receives a request for <code>/some/dir/foo</code>, if
<code>/some/dir</code> has <code>MultiViews</code> enabled, and
<code>/some/dir/foo</code> does <em>not</em> exist, then the
server reads the directory looking for files named foo.*, and
effectively fakes up a type map which names all those files,
assigning them the same media types and content-encodings it
would have if the client had asked for one of them by name. It
then chooses the best match to the client's requirements.</p>
<p><code>MultiViews</code> may also apply to searches for the file
named by the <directive
module="mod_dir">DirectoryIndex</directive> directive, if the
server is trying to index a directory. If the configuration files
specify</p>
<example>DirectoryIndex index</example>
<p>then the server will arbitrate between <code>index.html</code>
and <code>index.html3</code> if both are present. If neither
are present, and <code>index.cgi</code> is there, the server
will run it.</p>
<p>If one of the files found when reading the directory does not
have an extension recognized by <code>mod_mime</code> to designate
its Charset, Content-Type, Language, or Encoding, then the result
depends on the setting of the <directive
module="mod_mime">MultiViewsMatch</directive> directive. This
directive determines whether handlers, filters, and other
extension types can participate in MultiViews negotiation.</p>
</section>
</section>
<section id="methods"><title>The Negotiation Methods</title>
<p>After httpd has obtained a list of the variants for a given
resource, either from a type-map file or from the filenames in
the directory, it invokes one of two methods to decide on the
'best' variant to return, if any. It is not necessary to know
any of the details of how negotiation actually takes place in
order to use httpd's content negotiation features. However the
rest of this document explains the methods used for those
interested. </p>
<p>There are two negotiation methods:</p>
<ol>
<li><strong>Server driven negotiation with the httpd
algorithm</strong> is used in the normal case. The httpd
algorithm is explained in more detail below. When this
algorithm is used, httpd can sometimes 'fiddle' the quality
factor of a particular dimension to achieve a better result.
The ways httpd can fiddle quality factors is explained in
more detail below.</li>
<li><strong>Transparent content negotiation</strong> is used
when the browser specifically requests this through the
mechanism defined in RFC 2295. This negotiation method gives
the browser full control over deciding on the 'best' variant,
the result is therefore dependent on the specific algorithms
used by the browser. As part of the transparent negotiation
process, the browser can ask httpd to run the 'remote
variant selection algorithm' defined in RFC 2296.</li>
</ol>
<section id="dimensions"><title>Dimensions of Negotiation</title>
<table>
<columnspec><column width=".15"/><column width=".85"/></columnspec>
<tr valign="top">
<th>Dimension</th>
<th>Notes</th>
</tr>
<tr valign="top">
<td>Media Type</td>
<td>Browser indicates preferences with the <code>Accept</code>
header field. Each item can have an associated quality factor.
Variant description can also have a quality factor (the "qs"
parameter).</td>
</tr>
<tr valign="top">
<td>Language</td>
<td>Browser indicates preferences with the
<code>Accept-Language</code> header field. Each item can have
a quality factor. Variants can be associated with none, one or
more than one language.</td>
</tr>
<tr valign="top">
<td>Encoding</td>
<td>Browser indicates preference with the
<code>Accept-Encoding</code> header field. Each item can have
a quality factor.</td>
</tr>
<tr valign="top">
<td>Charset</td>
<td>Browser indicates preference with the
<code>Accept-Charset</code> header field. Each item can have a
quality factor. Variants can indicate a charset as a parameter
of the media type.</td>
</tr>
</table>
</section>
<section id="algorithm"><title>httpd Negotiation Algorithm</title>
<p>httpd can use the following algorithm to select the 'best'
variant (if any) to return to the browser. This algorithm is
not further configurable. It operates as follows:</p>
<ol>
<li>First, for each dimension of the negotiation, check the
appropriate <em>Accept*</em> header field and assign a
quality to each variant. If the <em>Accept*</em> header for
any dimension implies that this variant is not acceptable,
eliminate it. If no variants remain, go to step 4.</li>
<li>
Select the 'best' variant by a process of elimination. Each
of the following tests is applied in order. Any variants
not selected at each test are eliminated. After each test,
if only one variant remains, select it as the best match
and proceed to step 3. If more than one variant remains,
move on to the next test.
<ol>
<li>Multiply the quality factor from the <code>Accept</code>
header with the quality-of-source factor for this variants
media type, and select the variants with the highest
value.</li>
<li>Select the variants with the highest language quality
factor.</li>
<li>Select the variants with the best language match,
using either the order of languages in the
<code>Accept-Language</code> header (if present), or else
the order of languages in the <code>LanguagePriority</code>
directive (if present).</li>
<li>Select the variants with the highest 'level' media
parameter (used to give the version of text/html media
types).</li>
<li>Select variants with the best charset media
parameters, as given on the <code>Accept-Charset</code>
header line. Charset ISO-8859-1 is acceptable unless
explicitly excluded. Variants with a <code>text/*</code>
media type but not explicitly associated with a particular
charset are assumed to be in ISO-8859-1.</li>
<li>Select those variants which have associated charset
media parameters that are <em>not</em> ISO-8859-1. If
there are no such variants, select all variants
instead.</li>
<li>Select the variants with the best encoding. If there
are variants with an encoding that is acceptable to the
user-agent, select only these variants. Otherwise if
there is a mix of encoded and non-encoded variants,
select only the unencoded variants. If either all
variants are encoded or all variants are not encoded,
select all variants.</li>
<li>Select the variants with the smallest content
length.</li>
<li>Select the first variant of those remaining. This
will be either the first listed in the type-map file, or
when variants are read from the directory, the one whose
file name comes first when sorted using ASCII code
order.</li>
</ol>
</li>
<li>The algorithm has now selected one 'best' variant, so
return it as the response. The HTTP response header
<code>Vary</code> is set to indicate the dimensions of
negotiation (browsers and caches can use this information when
caching the resource). End.</li>
<li>To get here means no variant was selected (because none
are acceptable to the browser). Return a 406 status (meaning
"No acceptable representation") with a response body
consisting of an HTML document listing the available
variants. Also set the HTTP <code>Vary</code> header to
indicate the dimensions of variance.</li>
</ol>
</section>
</section>
<section id="better"><title>Fiddling with Quality
Values</title>
<p>httpd sometimes changes the quality values from what would
be expected by a strict interpretation of the httpd
negotiation algorithm above. This is to get a better result
from the algorithm for browsers which do not send full or
accurate information. Some of the most popular browsers send
<code>Accept</code> header information which would otherwise
result in the selection of the wrong variant in many cases. If a
browser sends full and correct information these fiddles will not
be applied.</p>
<section id="wildcards"><title>Media Types and Wildcards</title>
<p>The <code>Accept:</code> request header indicates preferences
for media types. It can also include 'wildcard' media types, such
as "image/*" or "*/*" where the * matches any string. So a request
including:</p>
<example>Accept: image/*, */*</example>
<p>would indicate that any type starting "image/" is acceptable,
as is any other type.
Some browsers routinely send wildcards in addition to explicit
types they can handle. For example:</p>
<example>
Accept: text/html, text/plain, image/gif, image/jpeg, */*
</example>
<p>The intention of this is to indicate that the explicitly listed
types are preferred, but if a different representation is
available, that is ok too. Using explicit quality values,
what the browser really wants is something like:</p>
<example>
Accept: text/html, text/plain, image/gif, image/jpeg, */*; q=0.01
</example>
<p>The explicit types have no quality factor, so they default to a
preference of 1.0 (the highest). The wildcard */* is given a
low preference of 0.01, so other types will only be returned if
no variant matches an explicitly listed type.</p>
<p>If the <code>Accept:</code> header contains <em>no</em> q
factors at all, httpd sets the q value of "*/*", if present, to
0.01 to emulate the desired behavior. It also sets the q value of
wildcards of the format "type/*" to 0.02 (so these are preferred
over matches against "*/*". If any media type on the
<code>Accept:</code> header contains a q factor, these special
values are <em>not</em> applied, so requests from browsers which
send the explicit information to start with work as expected.</p>
</section>
<section id="exceptions"><title>Language Negotiation Exceptions</title>
<p>New in httpd 2.0, some exceptions have been added to the
negotiation algorithm to allow graceful fallback when language
negotiation fails to find a match.</p>
<p>When a client requests a page on your server, but the server
cannot find a single page that matches the
<code>Accept-language</code> sent by
the browser, the server will return either a "No Acceptable
Variant" or "Multiple Choices" response to the client. To avoid
these error messages, it is possible to configure httpd to ignore
the <code>Accept-language</code> in these cases and provide a
document that does not explicitly match the client's request. The
<directive
module="mod_negotiation">ForceLanguagePriority</directive>
directive can be used to override one or both of these error
messages and substitute the servers judgement in the form of the
<directive module="mod_negotiation">LanguagePriority</directive>
directive.</p>
<p>The server will also attempt to match language-subsets when no
other match can be found. For example, if a client requests
documents with the language <code>en-GB</code> for British
English, the server is not normally allowed by the HTTP/1.1
standard to match that against a document that is marked as simply
<code>en</code>. (Note that it is almost surely a configuration
error to include <code>en-GB</code> and not <code>en</code> in the
<code>Accept-Language</code> header, since it is very unlikely
that a reader understands British English, but doesn't understand
English in general. Unfortunately, many current clients have
default configurations that resemble this.) However, if no other
language match is possible and the server is about to return a "No
Acceptable Variants" error or fallback to the <directive
module="mod_negotiation">LanguagePriority</directive>, the server
will ignore the subset specification and match <code>en-GB</code>
against <code>en</code> documents. Implicitly, httpd will add
the parent language to the client's acceptable language list with
a very low quality value. But note that if the client requests
"en-GB; q=0.9, fr; q=0.8", and the server has documents
designated "en" and "fr", then the "fr" document will be returned.
This is necessary to maintain compliance with the HTTP/1.1
specification and to work effectively with properly configured
clients.</p>
<p>In order to support advanced techniques (such as cookies or
special URL-paths) to determine the user's preferred language,
since httpd 2.0.47 <module>mod_negotiation</module> recognizes
the <a href="env.html">environment variable</a>
<code>prefer-language</code>. If it exists and contains an
appropriate language tag, <module>mod_negotiation</module> will
try to select a matching variant. If there's no such variant,
the normal negotiation process applies.</p>
<example><title>Example</title>
SetEnvIf Cookie "language=(.+)" prefer-language=$1<br />
Header append Vary cookie
</example>
</section>
</section>
<section id="extensions"><title>Extensions to Transparent Content
Negotiation</title>
<p>httpd extends the transparent content negotiation protocol (RFC
2295) as follows. A new <code>{encoding ..}</code> element is used in
variant lists to label variants which are available with a specific
content-encoding only. The implementation of the RVSA/1.0 algorithm
(RFC 2296) is extended to recognize encoded variants in the list, and
to use them as candidate variants whenever their encodings are
acceptable according to the <code>Accept-Encoding</code> request
header. The RVSA/1.0 implementation does not round computed quality
factors to 5 decimal places before choosing the best variant.</p>
</section>
<section id="naming"><title>Note on hyperlinks and naming conventions</title>
<p>If you are using language negotiation you can choose between
different naming conventions, because files can have more than
one extension, and the order of the extensions is normally
irrelevant (see the <a
href="mod/mod_mime.html#multipleext">mod_mime</a> documentation
for details).</p>
<p>A typical file has a MIME-type extension (<em>e.g.</em>,
<code>html</code>), maybe an encoding extension (<em>e.g.</em>,
<code>gz</code>), and of course a language extension
(<em>e.g.</em>, <code>en</code>) when we have different
language variants of this file.</p>
<p>Examples:</p>
<ul>
<li>foo.en.html</li>
<li>foo.html.en</li>
<li>foo.en.html.gz</li>
</ul>
<p>Here some more examples of filenames together with valid and
invalid hyperlinks:</p>
<table border="1" cellpadding="8" cellspacing="0">
<columnspec><column width=".2"/><column width=".2"/>
<column width=".2"/></columnspec>
<tr>
<th>Filename</th>
<th>Valid hyperlink</th>
<th>Invalid hyperlink</th>
</tr>
<tr>
<td><em>foo.html.en</em></td>
<td>foo<br />
foo.html</td>
<td>-</td>
</tr>
<tr>
<td><em>foo.en.html</em></td>
<td>foo</td>
<td>foo.html</td>
</tr>
<tr>
<td><em>foo.html.en.gz</em></td>
<td>foo<br />
foo.html</td>
<td>foo.gz<br />
foo.html.gz</td>
</tr>
<tr>
<td><em>foo.en.html.gz</em></td>
<td>foo</td>
<td>foo.html<br />
foo.html.gz<br />
foo.gz</td>
</tr>
<tr>
<td><em>foo.gz.html.en</em></td>
<td>foo<br />
foo.gz<br />
foo.gz.html</td>
<td>foo.html</td>
</tr>
<tr>
<td><em>foo.html.gz.en</em></td>
<td>foo<br />
foo.html<br />
foo.html.gz</td>
<td>foo.gz</td>
</tr>
</table>
<p>Looking at the table above, you will notice that it is always
possible to use the name without any extensions in a hyperlink
(<em>e.g.</em>, <code>foo</code>). The advantage is that you
can hide the actual type of a document rsp. file and can change
it later, <em>e.g.</em>, from <code>html</code> to
<code>shtml</code> or <code>cgi</code> without changing any
hyperlink references.</p>
<p>If you want to continue to use a MIME-type in your
hyperlinks (<em>e.g.</em> <code>foo.html</code>) the language
extension (including an encoding extension if there is one)
must be on the right hand side of the MIME-type extension
(<em>e.g.</em>, <code>foo.html.en</code>).</p>
</section>
<section id="caching"><title>Note on Caching</title>
<p>When a cache stores a representation, it associates it with
the request URL. The next time that URL is requested, the cache
can use the stored representation. But, if the resource is
negotiable at the server, this might result in only the first
requested variant being cached and subsequent cache hits might
return the wrong response. To prevent this, httpd normally
marks all responses that are returned after content negotiation
as non-cacheable by HTTP/1.0 clients. httpd also supports the
HTTP/1.1 protocol features to allow caching of negotiated
responses.</p>
<p>For requests which come from a HTTP/1.0 compliant client
(either a browser or a cache), the directive <directive
module="mod_negotiation">CacheNegotiatedDocs</directive> can be
used to allow caching of responses which were subject to
negotiation. This directive can be given in the server config or
virtual host, and takes no arguments. It has no effect on requests
from HTTP/1.1 clients.</p>
<p>For HTTP/1.1 clients, httpd sends a <code>Vary</code> HTTP
response header to indicate the negotiation dimensions for the
response. Caches can use this information to determine whether a
subsequent request can be served from the local copy. To
encourage a cache to use the local copy regardless of the
negotiation dimensions, set the <code>force-no-vary</code> <a
href="env.html#special">environment variable</a>.</p>
</section>
</manualpage>