Question

I am not a master of Apache/.htaccess.

For a URL: base-url/destination/, I need to show the page in that language which saved in the cookie. If no language us saved in the cookie or if the cookie is not present, I need to show the page same as that of browser's language.

Now, in order to achieve this, I wrote rules in .htaccess file as:

RewriteCond %{REQUEST_URI} /destination/$ 
RewriteCond %{HTTP_COOKIE} language_uiAAA(\w{3}).*?BBB
RewriteRule ^(.*)\$ $httpbase/%1/destination.html

This properly shows the page in the language saved in the cookie.

But, I've been asked to write rules using SetEnvIf, SetEnvIfNoCase or prefer-language.

Hence, I googled and wrote these rules:

SetEnvIf ^Cookie$ "(.+)" HTTP_MY_COOKIE=$1
SetEnvIf HTTP_MY_COOKIE ".+language_uiAAA(.+?)BBB.+" saved_language=$1
SetEnvIf saved_language "^(.+)$" prefer-language=$1

But,these rules aren't working. Please help.

OR

Please suggest a better way.

Was it helpful?

Solution

Voila!

Done!!

Here is the code:

SetEnvIf ^Cookie$ "(.+)" HTTP_MY_COOKIE=$1
SetEnvIf HTTP_MY_COOKIE ".+language_uiAAA(.+?)BBB.+" saved_language=$1
SetEnvIf saved_language "^(\w{2}).+$" prefer-language=$1
SetEnvIf REDIRECT_prefer-language (.+) prefer-language=$1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top