How do I make RewriteCond %{HTTP_COOKIE} match a cookie value exactly? [duplicate]

Solution 1:

There could be of course several cookies, and they can be URL escaped, making comparisons tricky.

Something like this ought to work in most cases:

RewriteCond %{HTTP_COOKIE}     its=([^;]+) 
RewriteCond %1                 ^me$
RewriteRule ......

If you need to unescape the cookie you can add a rewritemap for that:

RewriteMap unescape int:unescape

RewriteCond %{HTTP_COOKIE}     its=([^;]+) 
RewriteCond %{unescape:%1}     ^me$
RewriteRule ......

Solution 2:

It doesn't need to be more complicated than

RewriteCond %{HTTP_COOKIE}     /^(.*;)?its=me(;.*)?$/

Note that if the cookie value contains special (not URL-safe) characters, Krist van Besien's solution probably works best.