Question:
I'm developing a website that needs to be available in 3 languages (only static content, to be served by Apache). I would like to make use of the language detection features so that the user is already in a version compatible with the language preferences of his browser , but I also want him to be able to choose a different language through special links. Is it possible to do this from Apache, without using some server-side language?
I started reading the documentation on content negotiation and mod_negotiation
, but I'm quite lost as the examples given don't seem clear to me. What I understand so far (please correct me if I'm wrong) is the following:
- I must create my pages according to a specific convention, eg
index.html.pt
,index.html.en
,index.html.ja
; -
I must configure
Directory
to enable content negotiation:<Directory /var/www/vhosts/example.com/httpdocs> Options Indexes FollowSymLinks MultiViews DirectoryIndex index.html AllowOverride None Order allow,deny allow from all LanguagePriority en pt ja ForceLanguagePriority Prefer Fallback </Directory>
-
Each page must link to the base name, without specifying the language, eg:
href="index.html"
.
With that, from what I understand the server will be able to choose a version based on the Accept-Language
header that the browser sends. If this is correct, the first part is ok.
However, I have no idea how through a link I could change the current language of the page – and make it so that it remains the current language even after the user clicks on other links. The documentation mentions "advanced techniques (like cookies or special URL-paths)", but I couldn't understand what it was all about, and the linked documentation didn't help much. Is there a simple way to do this? Give preference, without needing JavaScript (but if there's no other way, that's fine).
Answer:
According to the W3C there is a certain ambiguity in mod_negotiation follow the link When to use language negotiation just to point out there is also Apache HTTPD exploit mod_negotiation Filename Bruter
but another alternative to get around this would be to set a cookie for each index
index.html.en
<html>
<head>
<title>ingles</title>
<meta http-equiv="Set-Cookie" content="lang=en; path=/en;>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<h1>bem vindo ao site do ingles</h1>
</body>
</html>
index.html.pt
<html>
<head>
<title>portuguse</title>
<meta http-equiv="Set-Cookie" content="lang=en; path=/pt;">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<h1>Bem vindo ao Site do portuga!</h1>
</body>
</html>
index.html.jp
<html>
<head>
<title>japones</title>
<meta http-equiv="Set-Cookie" content="lang=en; path=/jp;">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<h1>bem vindo ao site do japa</h1>
</body>
</html>
and create a directory for each
http://mydomain.com/en/
http://mydomain.com/pt/
http://mydomain.com/jp/
now and just add the following lines to your apache
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{HTTP_COOKIE} lang=([^;]+)
RewriteRule .* http://mydomain.com/%1 [R=302,L]
FONTES:
How to use the html tag HTTP-EQUIV “SET-COOKIE”
Using Apache2 Content Negotiation To Serve Different Languages
Check cookie and redirect with apache