htaccess的重定向只有在HTTP引用不等于什么(htaccess Redirect only

2019-09-21 22:26发布

我有以下规则致力于中国用户重定向到该网站的中国语言版本:

  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)的链接。

但是,这只是重定向他们直接回到中国的网站,因为它的规则相匹配。 所以我需要它,因此上述规则仅触发如果引用者是不是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]

但是,这是行不通的。

Answer 1:

当用户在英文网站查看他们的第二页关于什么的,它会重定向到中国。 更好的方法是设置一个cookie,然后检查一下:

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



文章来源: htaccess Redirect only if the HTTP referrer does NOT equal something