htaccess Redirect only if the HTTP referrer does NOT equal something

≡放荡痞女 提交于 2019-12-08 01:22:31

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/12184543/htaccess-redirect-only-if-the-http-referrer-does-not-equal-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!