apachemod-rewritemultilingualmod-negotiation

Serving multi-language static content via Apache for a Web Application


I have some multi-language static content which I wish to serve via Apache. The files are cache files generated JIT by the app so if they're there, they should be served, otherwise forward the request to application which will create them in-place.

I specify the language via HTTP's Accept-Language header which can be hr, en, but also en-US,en;q=0.5 (so there isn't a simple "append language to filename to proceed").

The URLs are like:

in my app I have a folder with the exactly the same layout, only the language gets appended, like this:

/static/homepage.html.en
/static/homepage.html.hr
/static/script.js.en
/static/script.js.hr

I've figured out that for the Accept-Language parsing I need mod_negotiation and came up with this:

# Alias
/static /www/app/var/cache/static
<Directory /www/app/var/cache/static>
  FileETag MTime Size
  Options MultiViews
  AddLanguage hr .hr
  AddLanguage en .en
  <IfModule mod_negotiation.c>
    LanguagePriority en hr
  </IfModule>      

  ErrorDocument 404 /index.php
</Directory>

It works, but only for the first miss. So

  1. if I request http://www.example.com/static/homepage.html, Accept-Language: hr, it will create homepage.html.hr (correct)
  2. Now, if I request http://www.example.com/static/homepage.html, Accept-Language: en, it will not thrown a 404 (by which triggering a file generating process) but instead serve homepage.html.hr (which is close enough for Apache, but incorrect for me).

The solutions I see for this:

Is there a fourth way or something I'm missing?

Edit: the complete solution as per the accepted answer

# Alias
/static /www/app/var/cache/static
<Directory /www/app/var/cache/static>
  FileETag MTime Size
  Options MultiViews
  AddLanguage hr .hr
  AddLanguage en .en
  <IfModule mod_negotiation.c>
    LanguagePriority en hr
    ForceLanguagePriority none
  </IfModule>      

  ErrorDocument 404 /index.php
  ErrorDocument 406 /index.php
</Directory>

Solution

  • Perhaps ForceLanguagePriority is set to "Prefer" or "Prefer Fallback". If you set it instead to "None", Apache will generate a 406 error, which you could configure to trigger the same file generating process as the 404.