0

Let me explain the problem. We have a website in 2 languages: fr & nl (dutch).

When you arrive on our website, you land on www.domain.be which redirects you (as you can see in the following code) to the dutch version if your browser language is set to 'nl' and if it's set to 'en' (because dutch people often use this language for they browser) or leave you on www.domain.be / fr.domain.be (both url are work to call the website - the fr... one is more in response of the nl... one)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R] 
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>

On the website, you can choose, by clicking on 2 links (in the top right corner), if you want to go to fr.domain.be or nl.domain.be. When you click on one of those link, the htaccess redirects you even if you want to go to the fr part (while navigating on the dutch one) and the same on the nl part.

How can I solve that? I would like the htaccess to only redirect you when you first come to the website but then be desactivated and allow the user to choose his language if he wants to.

Could you please help me? I'm on this for like two days...

Simon
  • 29
  • 1
  • 5
  • Use cookies, this might help you: http://stackoverflow.com/questions/3978726/how-to-do-htaccess-redirect-based-on-cookie-value – jdu May 30 '12 at 18:47

2 Answers2

1

You're only redirecting the site base /, so swith directly to another page should not be a problem.

Can't you just use a fake index page when you manually switch ? Like http://nl.domain.be/index

zessx
  • 68,042
  • 28
  • 135
  • 158
  • Maybe it's because the links used on the top right corner are like http://fr.domain.be http://nl.domain.be Following what you say, I should rather use http://fr(or nl).domain.be/index ? – Simon May 30 '12 at 19:09
  • I guess so. Your redirection should not be effective for fr.domain.be/index – zessx May 30 '12 at 19:11
  • in fact, the problem was that I used fr.domain.be I tried with what you said but it didn't work. I finally tried fr.domain.be/index.php and it work. Thank you for your help. – Simon May 30 '12 at 19:18
  • Yes sorry, fr.domain.be/index won't be naturally recognized (there's no file named `index` without extension in your folder). – zessx May 30 '12 at 19:22
0

You can create multiple conditions for a rule set so you could add an aditional check that the redirect only happens when you do not match the HTTP_REFERER to your domains.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^nl [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC] 
RewriteRule ^$ http://nl.domain.be/ [L,R] 
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*\.domain\.be/ [NC] 
RewriteRule ^$ http://nl.domain.be/ [L,R]
</IfModule>

the ! means NOT so basically you are saying when the HTTP_REFERER does NOT match the url pattern, which is "(wildcard).domain.be".

This will prevent the rule from being run if they are on your site currently and trying to change the language.

NOTE: I'm not near an apache box to test this so my syntax might be off but that should get you down the right path.

Alan Barber
  • 983
  • 5
  • 15