Score:0

Apache 2.4: How to redirect user depending on language preferences

jp flag

I have a multilingual static site. The site is structured such that all content is in English by default so the default content resides under /. For example:

Path:           Content:
/               Home
/contact.html   Contact Page
/services.html  Services
/about.html     About us

And then there is a subfolder for each supported language. For example 'de' for German, 'da' for Danish, etc. All the localized content will then resize in the subfolder for the corresponding language.

So the Danish content will be structured like this:

Path:           Content:
/da/               Home
/da/contact.html   Contact Page
/da/services.html  Services
/da/about.html     About us

And likewise for other languages.

Since this is a completely static site, I cannot use some scripting language like PHP to detect the user locale and redirect them based on that.

So I figure I will have to let apache do this part.

I am using Apache 2.4.

My question is, then, how do I configure the Apache virtual host such that users who request / will be redirected to /da/ if their preferred language is Danish, to /de/ if their preferred language is German, and just get / as default if English or any other language is preferred language? Likewise, if a user requests /foo.html, they will be redirected to /$LANG/foo.html, where $LANG is their preferred language (is that language is on the list of supported languages).

User locale/language preference will be detected using the client's Accept-Language header.

Now, this header will support the client specifying multiple languages in a prioritized order. So we will just use whatever is preferred.

Also, I guess we will need to store the language in a cookie of some sort. Because if the user has specified German as preferred language but then explicitly requests the Danish or English version then we should not keep redirecting them.

So I tried this. This does not set a cookie but it only triggers on the main page (/). It just checks if Danish is in Accept-Language. But not if Danish is preferred or not. Also, it does not support multiple languages really well. For example lets say we have a user that preferres German but has Danish as second language. They would get Danish here and not German.

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (da) [NC]
RewriteCond %{REQUEST_URI} "^/$"
RewriteRule ^(.*)$ https://%{SERVER_NAME}/da [R=303,NC,L]   
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.