3

In a digital archaeology project I have found a web server directory with files dated from 2001 that had a peculiar collection of index.html files with names ending with two letter codes of different languages and language charsets, i.e.

index.html.ca       index.html.ja.jis   index.html.ru.iso-ru
index.html.cz       index.html.kr.iso-kr    index.html.ru.koi8-r
index.html.de       index.html.lb.utf8  index.html.ru.ucs2
index.html.dk       index.html.nl       index.html.ru.ucs4
index.html.ee       index.html.nn       index.html.ru.utf8
...

How do I configure modern Apache to serve different static pages based on the value of Accept-Language header?

2

Those files are almost certainly meant to be served via Apache content negotiation. Since there does not appear to be a .var file to serve as an explicit type map, and since the file name extensions correspond to standard language and charset codes, they're probably meant to be used with the implicit MultiViews mechanism.

To enable MultiViews for the directory containing these files, add the following directive into an .htaccess file in that directory (or into a matching <Directory> section in the global Apache config):

Options +MultiViews

This will make Apache automatically resolve requests for nonexistent files by appending additional extensions based on the language, MIME type and charset preferences indicated by the HTTP Accept, Accept-Charset and Accept-Language headers. For example, a request for index.html might cause Apache to instead return the contents of index.html.de or index.html.ja.jis or index.html.ru.utf8, depending on the preferred language(s) configured in the user's browser.

Note that, in addition to content negotiation, you should also provide some way for users to explicitly select the version of the page that they prefer. When using MultiViews, this can be as simple as providing direct links to the translated pages with the language code included.

2

I would recommend issuing redirects so that search engines can view unique urls for each language. Here are some rewrite rules that you could use in your .htaccess file:

RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^index.html$ /index.html.de [L,R=302]
RewriteCond %{HTTP:Accept-Language} ^ru [NC]
RewriteRule ^index.html$ /index.html.ru.utf8 [L,R=302]
...
  • NC is "no case" (case insensitive)
  • L is "last" (stop processing other rules)
  • R=302 is a temporary redirect

You would still need a default index.html for browsers that don't send the header with a recognized value.

  • 2
    A problem with this solution (besides the fact that it's kind of an ugly hack) is that it doesn't respect the preference order of the languages listed in the Accept-Language header. For example, I can sort of read German and French, but would much prefer English (or Finnish or Swedish), so the list of preferred languages I've configured for my browser is something like fi > en > sv > de > fr. Someone else may have that same list, but in a different order. Your example code would e.g. always pick the German version before the Russian one, if the user's browser indicates they'll accept either. – Ilmari Karonen Sep 28 '16 at 10:20
  • There is a caret in front of the match (meaning "starts with"), so it will always choose your first choice. The place where it won't do the right thing is when your preferred language is not supported but your second is. When you redirect, that is less of an issue anyway because search engines will send traffic directly to the correct language. – Stephen Ostermiller Sep 28 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.