Forum Moderators: phranque
The content negotiation works as usual, but since Apace 1.3 doesn't have the "prefer-language" variable it seems to be impossible to override the negotiated language.
I am looking for a (hopefully) simple solution which doesn't involve too much re-writing.
Here's the setup:
All pages have a name like: name.xx.html where xx is the language code.
All these pages reside in the root directory.
The root .htaccess file contains:
Options +MultiViews
AddLanguage en .en
AddLanguage nl .nl
AddLanguage tr .tr
LanguagePriority en nl tr
#ForceLanguagePriority Prefer Fallback
#SetEnvIf Cookie "language=(.+)" prefer-language=$1
#Header append Vary cookie
The last three lines are commented out, since these don't work in 1.3.
On each page I have a choice of language like so:
<li class="language"><a href="modules/change_language.php?lang=en&page=<!--#echo var="DOCUMENT_NAME"-->" lang="en" hreflang="en" title="This page in English">English</a></li>
<li class="language"><a href="modules/change_language.php?lang=nl&page=<!--#echo var="DOCUMENT_NAME"-->" lang="nl" hreflang="nl" title="Deze pagina in het Nederlands">Nederlands</a></li>
<li class="language hidden"><a href="modules/change_language.php?lang=tr&page=<!--#echo var="DOCUMENT_NAME"-->" lang="tr" hreflang="tr" title="Bu sayfa Türkçede">Türkçe</a></li>
With the contents of change_language.php being:
<?php
$chosenLanguage = $_GET['lang'];
$currentPage = $_GET['page'];
setcookie('language', $chosenLanguage, time()+60*60*24*365, '/');
$currentPage = preg_replace('/\..*/', '', $currentPage);
header('Location: SERVER_NAME' . $currentPage);
?>
All this works perfectly on apache 2.x, but any help on how to convert this to 1.3. is much appreciated. I was thinking in terms of rewrite? But I am not very good at that.
As a result, I'd recommend passing all potentially-unresolvable requests to a script, and let it figure out what language to serve. Or alternatively, upgrading this 'new' server to Apache 2.x.
If you must do this at the server level, look into using RewriteMap to invoke a small script (e.g. PERL) during the URL-to-filepath translation phase. This map must be defined at the server configuration level before it can then be used in .htaccess.
Also, if you have a choice, put as much of your code in the server config files as you can; It will be processed much more efficiently there than in .htaccess.
Jim
First off, I don't have access to the config files so I am stuck with the htaccess. For the same reason I cannot upgrade to 2.x
Secondly, I think I wasn't clear enough on the problem I am having. The automatic language selection works as expected, It is the "manual override" that doesn't work.
The server does serve the user the right language, and defaults to "en" if nothing or rubbish is present in the headers. So that works ok.
But... on each page there are links to all the other languages (in case someone wants to manually switch language). And these don't work under 1.3 as they do under 2.x.
In 2.x. you can "read" cookies in htaccess, but this cannot be done in 1.3, I am looking for an alternative to that.
Use RewriteCond %{HTTP_COOKIE} <regex-pattern>
Apache2.x added the [CO=] flag to RewriteRule which allows you to set cookies on the server response. In Apache 1.x you can use mod_headers to do it, if you can arrange things such that the Header Set directive can be conditioned based only on placing it in a <Files> or <FilesMatch> container. That is, making the cookie setting function conditional is the main limitation in Apache 1.3.x.
Jim
RewriteEngine on
#
RewriteCond %{HTTP_COOKIE} ^([^&]*&)*language="en"(&.*)?$
RewriteCond $1 !\.en\.html$
RewriteRule ^(.*)$ /$1.en.html [L]
Jim
[added] Corrected as noted below [/added]
[edited by: jdMorgan at 2:37 pm (utc) on Oct. 22, 2009]