Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

regex - htaccess compare cookie value and redirect if evaluation returns true/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.

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]

What I would like, but in .htaccess style:

if ($_COOKIE['cookie_name'] != 'specific_value'){
//rewrite_rule or deny from all.
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...