Htaccess compares cookie value and redirects if evaluation returns true / false

I would like someone to help me with an if-else statement in htaccess. I want htaccess to read a cookie and check if its value is equal to a certain value. If it evaluates to false, it should exclude redirection and prevent access to the requested folder. Maybe giving up on everyone would be better if evaluating returns false.

I know that the following code checks to see if the set cookie value is set. If it is not installed, it will execute the rewrite rule below it. But how can I tweak this string so that it checks if it is equal to a specific 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. } 
+7
regex cookies apache .htaccess mod-rewrite
source share
3 answers

You're close Cookie string requires = :

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

Replace required_value with the value you want to map.

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

;? ensures that a match occurs when there are several pairs of cookies, and in the case when cookie_name is the only set of cookies. It also prevents cookie values ​​from matching, such as off , when only of is required (substring).

+5
source share

You can use this code that validates a specific value in a cookie:

 RewriteEngine On RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value [NC] RewriteRule ^ http://www.google.com [NC,L,R=302] 
+1
source share

All Articles