htaccess 比较 cookie 值并在评估返回 true/false 时重定向 [英] htaccess compare cookie value and redirect if evaluation returns true/false

查看:22
本文介绍了htaccess 比较 cookie 值并在评估返回 true/false 时重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人帮助我处理 htaccess 中的 if-else 语句.我想要的是 htaccess 读取 cookie 并检查其值是否等于定义的值.如果它评估为假,它应该执行重定向并阻止访问请求的文件夹.如果评估返回 false,也许所有人都拒绝会更好.

I would like somebody to help me with an if-else statement in htaccess. What I want is htaccess to read a cookie and check if its value equals a defined value. If it evaluates to false, it should excecute a redirect and prevent from the requested folder to be accessed. Maybe a deny from all would be better if the evaluation returns false.

我知道以下代码会检查是否设置了命名 cookie 值.如果没有设置,就会执行下面的重写规则.但是我如何调整这条线以检查它是否等于某个值?

I know that the following code checks if a named cookie value is set. If it is not set, it will execute the rewrite rule below it. But how can I adjust this line so that it checks if it equals a certain value?

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !^.*cookie_name.*$ [NC]
RewriteRule .* http://www.google.com [NC,L]

我想要什么,但采用 .htaccess 风格:

What I would like, but in .htaccess style:

if ($_COOKIE['cookie_name'] != 'specific_value'){
//rewrite_rule or deny from all.
}

推荐答案

离你很近了.cookie 字符串需要一个 =:

You're close. The cookie string needs a =:

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^ http://www.google.com [NC,L]

这篇关于htaccess 比较 cookie 值并在评估返回 true/false 时重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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