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]
0 Answers