How to serve a multi-language site in Apache?

14

I'm developing a website that needs to be available in 3 languages (static content only, to be served by Apache). I would like to make use of the language detection features so that the user already fell into a version compatible with the language preferences of his browser , but also want him to choose a different language through special links . Is it possible to do this by Apache without the use of some server-side language?

I started reading the documentation on content negotiation and about mod_negotiation , but I'm pretty lost, because the examples given do not seem clear to me. What I understood so far (please correct me if I'm wrong) was as follows:

  • I must create my pages according to a specific convention, eg index.html.pt , index.html.en , index.html.ja ;
  • I should set 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 should link to the base name, without specifying the language, eg href="index.html" .

With this, from what I understand the server will be able to choose a version based on the header Accept-Language that the browser sends. If that is correct, the first part is ok.

However, I have no idea how I could over a link change the current language of the page - and do it so that it continues to be the current language even after the user clicks on other links. The documentation mentions "advanced techniques (such as cookies or special URL-paths)," but I could not understand what it was, and the linked documentation did not help much. Is there a simple way to do this? Give preference, without needing JavaScript (but if you do not have another way, that's fine).

    
asked by anonymous 02.06.2016 / 18:34

2 answers

4

According to W3C there is a certain ambiguity in mod_negotiation follows the When to use language negotiation just to point out also exploit Apache HTTPD mod_negotiation Filename Bruter

But another alternative to circumvent 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, 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]

SOURCES:

How to use the html tag HTTP-EQUIV "SET-COOKIE"

Using Apache2 Content Negotiation To Serve Different Languages

Check cookie and redirect with apache

    
16.06.2016 / 03:04
2

First of all you should change the names of your files to end in .html, it should be to type index.pt.html

Then you can use mod_rewrite and mod_geoip to redirect traffic from certain ips per country to the desired pages.

link

Add to apache on Ubuntu

# apt-get install libapache2-mod-geoip

Install the module

# apache2ctl -M | grep -E "geoip|rewrite"
geoip_module (shared)
rewrite_module (shared)

Restart to apache

# service apache2 restart

Then edit your .htaccess and put

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(PT|BR)$
RewriteRule ^(.*)$ http://example.com/index.$1.html [L]
    
15.06.2016 / 11:37