2

I have the following rules working to redirect chinese users to the chinese language version of the site:

  RewriteCond %{HTTP:Accept-Language} ^zh [NC]
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  RewriteRule ^(.*)$ http://ch.example.com/$1 [L,R=301]

  RewriteCond %{HTTP:Accept-Language} ^zh [NC]
  RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  RewriteRule ^(.*)$ http://ch.example.com/$1 [L,R=301]

This works great.

However, there is a link that is supposed to take them back to the english language version of the site, which is just the normal domain (www.example.com).

But this just redirects them straight back to the chinese site, because it matches the rules. So I need to make it so the above rules are trigger ONLY if the referer is NOT ch.example.com.

I tried something like this:

  RewriteCond %{HTTP:Accept-Language} ^zh [NC]
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  RewriteCond %{HTTP_REFERER} !^http?://ch\.example\.com/ [nc]
  RewriteRule ^(.*)$ http://ch.example.com/$1 [L,R=301]

But it doesn't work.

jazzdrive3
  • 335
  • 3
  • 13

1 Answers1

2

What about when the user views their second page on the English site, it will redirect to Chinese. A better method is to set a cookie and then check it:

For example: http://www.askapache.com/htaccess/htaccess-fresh.html#Set_Cookie_based_Requested_directory

Paul
  • 289
  • 3
  • 15
  • Good point. But we should be able to check in general if the referral is from *ggvc.com at all, and then not redirect, right? So, soemthing like this (even though this doesn't work) RewriteCond %{HTTP:Accept-Language} ^zh [NC] RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteCond %{HTTP_REFERER} !^http://ch\.example\.com/ [NC] RewriteCond %{HTTP_REFERER} !^http://www\.example\.com/ [NC] RewriteCond %{HTTP_REFERER} !^http://example\.com/ [NC] RewriteRule ^(.*)$ http://ch.example.com/$1 [L,R=301] – jazzdrive3 Aug 29 '12 at 21:59
  • HTTP_REFERER shouldn't be relied upon: "Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted." – Paul Aug 30 '12 at 11:34
  • How about instead setting the link to the English site to point to a page that sets the cookies and then redirects to the english site. – Paul Aug 30 '12 at 11:36
  • That's fine if it can't be trusted, I'm just trying to get it working for a majority of users, most of which are non-technical anyway, and using IE. – jazzdrive3 Aug 30 '12 at 14:20
  • Okay, try this: RewriteCond %{HTTP_REFERER} !^https?://ch\.example\.com [NC] – Paul Aug 30 '12 at 16:42