Using Apache2 Content Negotiation To Serve Different Languages
|
Submitted by falko (Contact Author) (Forums) on Tue, 2011-07-26 16:31. :: Debian | Web Server | Apache
Using Apache2 Content Negotiation To Serve Different LanguagesVersion 1.0 Content negotiation is the ability of a web server to deliver the document that best matches the browser's preferences/capabilities. For example, if a resource exists in multiple languages, the web server can choose which variant it serves based on the Accept-Language header delivered by the browser. This tutorial describes how to configure content negotiation in Apache2 to serve different languages based on browser preferences. I do not issue any guarantee that this will work for you!
1 Preliminary NoteIn Apache2, content negotiation is made available by the mod_negotiation module which is built-in by default. I will use a Debian Squeeze system here with Apache2 already installed, but the Apache configuration is independent of the distribution you use. On Debian Squeeze, there's a global content negotiation configuration in /etc/apache2/mods-available/negotiation.conf; because I want to demonstrate how to configure it on a per-vhost basis, I comment out everything in that file (of course, you can do your content negostiation configuration in this file instead of in your vhosts if you want the configuration to be valid for all your vhosts): vi /etc/apache2/mods-available/negotiation.conf
Restart Apache afterwards: /etc/init.d/apache2 restart I will modify Debian's default Apache vhost with the document root /var/www and the vhost configuration file /etc/apache2/sites-available/default in this tutorial. Let's delete any index file in /var/www... rm -f /var/www/index.* ... and create three new index files, one in German (index.html.de), one in English (index.html.en), and one in French (index.html.fr): vi /var/www/index.html.de
vi /var/www/index.html.en
vi /var/www/index.html.fr
Our /var/www directory looks as follows: ls -la /var/www/ root@server1:~# ls -la /var/www/ I want Apache to deliver the right document (variant) based on the browser's language preferences. Browsers send an Accept-Language header which lists their preferred language(s). This can be achieved in two ways:
2 MultiViewsMultiViews works as follows: if you request a resource named foo, and foo does not exist in the directory, Apache will search for all files named foo.*, like foo.html, foo.html.de, foo.html.en, foo.de.html, foo.en.html, foo.en.html.gz, foo.gz.en.html, foo.html.gz.en, foo.html.en.gz, and so on. MultiViews is a per-directory setting, i.e., it has to be set with an Options directive in a <Directory>, <Location>, or <Files> section in the Apache configuration. It has to be set explicitly (i.e. Options MultiViews); Options All does not set it. vi /etc/apache2/sites-available/default
That's basically all we need for content negotiation. Now let's look at our browser's language preferences (I use Firefox 5 here). Go to Tools > Options: Under Content, there is a Languages area where you can click on the Choose... button: In this example, I have en-US and en enabled (the order is important!): Now let's go to our default vhost (my server's IP address is 192.168.0.100, so I go to http://192.168.0.100). I have the LiveHTTPHeaders plugin for Firefox installed so that I can check the headers sent by Firefox. As you see, my browser sends the header Accept-Language: en-us,en;q=0.5 which means its first preference is en-US (if no q value (q = Quality/Priority) is present, this means q=1.0; q must be a value between 0 (lowest priority) and 1 (highest priority)). Because we have specified no file name in our request (just http://192.168.0.100 instead of http://192.168.0.100/index.html or http://192.168.0.100/foo.html), Apache will use the DirectoryIndex directive to search for an index file, and because we use DirectoryIndex index.html, it will search for index.html. Because index.html does not exist, MultiViews will now try to find the best match for the request; we don't have an index.html.en-US file, but we have an index.html.en file (en is the second-best preference of our browser), so index.html.en gets served: (Apache will also try to match language subsets if no other match can be found. For example, if our browser accepted only en-US, Apache would serve the en variant - index.html.en - instead of serving a 406 "Not Acceptable" error (en gets a low q value, but because the browser does not accept any other language, the en variant will be served). But if the browser sends an Accept-Language: en-us,fr;q=0.8 header, for example, the French variant - index.html.fr - will be served because of the low q value for en.)
|








Recent comments
15 hours 35 min ago
20 hours 40 min ago
1 day 1 hour ago
1 day 2 hours ago
1 day 17 hours ago
1 day 17 hours ago
1 day 22 hours ago
2 days 4 hours ago
2 days 5 hours ago
2 days 6 hours ago