0

So i searched the whole day yesterday for a solution to my problem, found similarities but nothing matching exactly. I try to write a .htacces file which is supposed to check the browser language and then redirect the user either to (GERMAN) xyz.de/?lang=de&page=$1 or (NONGERMAN) xyz.de/?lang=en&page=$1

It's supposed to automatically load the homepage in the browser's language. I got it so far that I don't get 500 errors any more and it works pretty fine with IE and does load the page in the correct language, which impresses me a lot. But in Firefox or Chrome it will show neither language nor the page; if I var_dump($_GET) I get nothing.

.htaccess

    RewriteEngine on
    RewriteCond %{HTTP_ACCEPT_LANGUAGE} ^(de|de-at|de-li|de-lu|de-ch) [NC]
    RewriteRule ^([^./]{3}[^.]*)$   /index.php?lang=de&page=$1 [QSA,L] 
    RewriteCond %{HTTP_ACCEPT_LANGUAGE} !^(de|de-at|de-li|de-lu|de-ch) [NC] 
    RewriteRule ^([^./]{3}[^.]*)$   /index.php?lang=en&page=$1 [QSA,L]<i>

[IE9] xyz.de

   var_dump($_GET)

array(2) { ["page"]=> string(0) "" ["lang"]=> string(2) "de" }

[GECKO] xyz.de

    var_dump($_GET)

array(0) { }

[GECKO] xyz.de/home

    var_dump($_GET)

Anybody an idea what the problem is????

array(2) { ["lang"]=> string(2) "en" ["page"]=> string(4) "home" }

// Change

Thanks to the Last Post i changed the Code following:

    RewriteCond %{HTTP:ACCEPT_LANGUAGE} !^(de) [NC]
    RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1&lang=en [QSA,L]

    RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1&lang=de [QSA,L]

now it redirects nothing, just when i put /home oder secondpage or what ever in it, it loads the page always in english and its supposed to redirect the page to xyz.de/lang=[ACCEPT-LANG]&page=home if you press in xyz.de

jeremy.k
  • 21
  • 3
  • Have you looked at the actual HTTP headers that are being sent by each oy your tested browsers? They must be the reason for the failure. – Perleone Dec 02 '12 at 11:40
  • I thought that, i hoped here is someone, that had once the same problem. The Headers definitly send out the Accept-Language: Chrome: Accept-Language:en-US,en;q=0.8 FireFox: Accept-Language: en-US,en;q=0.5 it looks like that FF&Chrome does write the Rewrite in to the Browser field but does not write the parameters in to $_GET// Sry for my Bad english btw. – jeremy.k Dec 02 '12 at 12:16
  • You can safely remove the second `RewriteCond`. If the frist matches the rewriting is terminated at the first `RewriteRule`. If not you want to catch all requests anyway... – arkascha Dec 02 '12 at 12:24
  • And I am a little confused by your comment here... You say FF & Chrome get their url shown in the location get rewriten? That suprises me by the look at your RewriteRules. There is no redirect in there. – arkascha Dec 02 '12 at 12:26
  • And what is that regex meant to express: `^([^./]{3}[^.]*)$` ? Everything that is _not_ starting with excactly three characters being either period (`.`) or slash (`/`) and followed by anything that does not contain a period (`.`) up to the end? Strange pattern... – arkascha Dec 02 '12 at 12:30
  • The Site im writing on is supposed to be bilingual, which means if someone from a German Language country calls the site it should direct to lang=de every otherone lang=en **bold**([^./] is supposed to mean not . or / honestly that pattern is out of a Book "CMS using JQuery and PHP" if you got a better solution let me know – jeremy.k Dec 02 '12 at 13:00
  • I also still dont understand why the browsers are the problem since the modrewrite works on the server. The IE is redirecting the right way; web335.s07.speicheranbieter.de redirects to web335.s07.speicheranbieter.de/index.php?lang=de&page="" Chrome & FF stays at web335.s07.speicheranbieter.de no redirect nor web335.s07.speicheranbieter.de/index.php?lang=de&page="" in the address field i am honestly new to all that mod_rewrite stuff but everybody has to start somehow – jeremy.k Dec 02 '12 at 13:02
  • Thanks to arkascha i thought i figured the problem out. the regex is definitly causing the problem, when i change the regex to (.*) it works in firefox and chrome but not anymore in IE. For crying out loud! – jeremy.k Dec 02 '12 at 14:19

1 Answers1

0

Try to use %{HTTP:Accept-Language} instead of %{HTTP_ACCEPT_LANGUAGE}.

This blog post is very useful.

Kamil Šrot
  • 2,141
  • 17
  • 19