How do I serve a site in multiple languages in Apache?

Asked

Viewed 240 times

16

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 already falls into a version compatible with the language preferences of your browser, but I also want him to be able to choose a different language through special links. It is possible to do this by Apache without using any language server-side?

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

  • I should create my pages according to a specific convention, ex.: index.html.pt, index.html.en, index.html.ja;
  • I must configure the Directory to enable content trading:

    <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, e.g..: href="index.html".

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

However, I have no idea how a link could change the current language of the page - and do so so so that it remains 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 couldn’t understand what it was 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 is no other way, fine).

  • To do directly in apache do not know, but it is also not necessary to javascript, can set cookies as soon as the page is sent in server-side language, depends on what you prefer, ie for example: meusite.com/pt (default if there are no cookies) has a link to meusite.com/en, there is soon part of the script (server side) that implements a cookie/session so that next times it is loaded meusite.com/en instead of meusite.com/en

  • I was noticing how the google algorithm works when it redirects from google.com to google.com or any other think it uses as criteria the ip and the accept-Language of the header of the requisicao think this would be the best way and put a language by default until there is a google application that automatically converts the language of the site without having to make changes on the pages so do not remember the name

  • 1

    Let’s wait for someone to clear up this doubt of yours now. :)

  • 1

    @drmcarvalho Thanks! This is no longer mere curiosity, I really need this for a job I’m doing. In the next few days, if I don’t have an answer I’ll search and try some options myself, and if I discover something useful I share here with the rest of the community.

2 answers

5

According to the W3C there is a certain ambiguity in mod_negotiation follows the link When to use language negotiation only 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 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]

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

  • 2

    Thank you for the answer! From what I understood, only the index would have special Urls (making it easy to change the language through a link), the rest would continue using mod_negotiation using the cookie set by the index. It seems feasible, I will do some tests to confirm. The material cited also looks interesting, I will give a deal calmer. + 1

3

First of all you must change the names of your files to end in . html, it must 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 intended pages.

https://linuxconfig.org/redirect-or-block-traffic-based-on-geographical-location-apache-geoip-mod

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]
  • Thanks for the reply, mod_geoip certainly brings an additional feature. Personally, I prefer the same mod_negotiation, since it takes into account the preferred language of the browser (and not only the physical location of the user).

  • @mgibsonbr Right, but think that many people use browsers in English even if they are in Br or Pt.

  • It is possible (by the way, I myself rsrs!), but whatever strategy is adopted, they will have false positives and false negatives. I suspect what to use negotiation as main and geoip as fallback would hit more often than not. But I could be wrong...

  • If you can do && then it is perfect. But this I don’t know. If not. You can do like steps an initial filter a case then redirect to another folder and filter through the other. But that’s just me theorizing. Not if it’s possible

Browser other questions tagged

You are not signed in. Login or sign up in order to post.