Content Negotiation

From DreamHost
Jump to: navigation, search

Content Negotiation

Content Negotiation allows a user to request different types of content from a web server according to their preferences and browser's capabilities. For example, it allows a web developer to serve English language documents to English speakers and French language pages to users who prefer French. Content Negotiation is not strictly limited to languages - it allows browsers that understand several image formats to choose the format which they prefer, e.g. GIF over JPEG or vice-versa.

The Apache web server supports two types of Content Negotiation, Type Maps and MultiViews. Type Maps are the preferred method since they consume fewer server resources.

Content Negotiation on Dreamhost

Type Maps

DreamHost supports Type Maps. You can enable Type Maps by creating a .htaccess file in the directory where you would like Content Negotiation that contains the following line:

AddHandler type-map var

You would then create a type map that has the extension .var, e.g. index.html.var which is the Apache default. To use the type mapping, you can link to the index.html.var file directly or, more scalably, add index.html.var to the list of default index types using the DirectoryIndex directive like so:

DirectoryIndex index.html.var index.html

If you use Server Side Includes, CGI or PHP scripts, or any type of index file other than HTML be sure to add the appropriate filename to the DirectoryIndex directive. Be aware that the server will open the first index file it finds! If you list index.php, index.html.var and index.html in that order, Apache will use the PHP file because it appears first.


DreamHost does support MultiViews. This Apache option allows you to do content negotiation based on file extension. To enable, add this line to your .htaccess file:

 Options +MultiViews

MultiViews on Content-Type

Here's how you can use MultiViews to serve xhtml files to user-agents supporting xhtml, and html files to those which doesn't. First, make sure Apache associates the file extensions with the content types:

 AddType text/html;charset=UTF-8 .html
 AddType application/xhtml+xml;charset=UTF-8;qs=0.999 .xhtml # low quality so that old browsers will get .html

Name the alternative versions of your files with the same prefix (for example, file.html and file.xhtml). If now you try to access just file, the server will negotiate the preferred version with the user agent.

MultiViews on language

The same method can be used to negotiate language. Like before, set up the proper file extensions:

 AddLanguage en .en       # English
 AddLanguage pt-br .pt-br # Brazilian Portuguese

Name your pages like file.en.html and, and the user will be served with the user agent's default language. Make sure the files have actual links to each other, though, as some people don't configure their browser's language preferences.

Combined Multiviews

You can combine any kind of content negotiation. If you have files named like file.en.html,, file.en.xhtml,, and set up .htaccess with all lines above, a link to file will automatically select the best content-type and the best language. It's also possible to do content negotiation on Charset (what the Unicode standard calls "encoding": utf-8, iso-8859-1, etc.) and Encoding (gzip, compress, deflate).

htaccess - Adding an extra extension

You can add an extra extension to be handled by a specific version of PHP by adding the following to your .htaccess file:

 AddHandler php5-cgi .htm

Change php5 to just php to use PHP4 instead of 5.

Further Reading

More information on Content Negotiation is available in the Apache Content Negotiation documentation.[1]

W3C has an informative FAQ on MultiViews.