htaccess仅在HTTP引荐来源网址不等于某值时才重定向 [英] htaccess Redirect only if the HTTP referrer does NOT equal something

查看:98
本文介绍了htaccess仅在HTTP引荐来源网址不等于某值时才重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下规则可以将中文用户重定向到网站的中文版本:

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]

这很好.

但是,应该有一个链接可以将其带回该网站的英语版本,该站点只是普通域名(www.example.com).

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).

但这只是将它们直接重定向回中文站点,因为它符合规则.因此,我需要这样做,以便仅当引荐人不是ch.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.

我尝试过这样的事情:

  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]

但这是行不通的.

推荐答案

当用户在英语网站上查看第二页时,它将重定向到中文.更好的方法是设置一个cookie,然后检查它:

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:

例如: http://www.askapache.com/htaccess/htaccess -fresh.html#Set_Cookie_based_Requested_directory

这篇关于htaccess仅在HTTP引荐来源网址不等于某值时才重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆