.htaccess to redirect if cookie value !== string

I want to be able to redirect all traffic (attempting to access a folder) to a page if a cookie with a given name is not set. So far I have:

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !^.*some-sort-of-cookie.*$ [NC]
RewriteRule .* /some-sort-of-error/ [NC,L,R=301]

This gives them access to the folder if a cookie named some-sort-of-cookie is set regardless of value. But it also works if the cookie contains some-sort-of-cookie rather than exactly equaling it. How can I change it so that it only works if it equals some-sort-of-cookie?

My regex is poor but I thought removing the .* would do it but that doesn’t work.


Been messing around with this and now have this code:

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !some-cookie-name=some-cookie-value [NC]
RewriteRule .* /some-sort-of-error/ [NC,L,R=301]

It seems to take the cookie name and value and treats it as a string cookie-name=cookie-value. It still seems to only check if the string contains cookie-name=cookie-value rather than equals to. Is this normal behaviour or is there any way to get it to match exactly? Is HTTP_COOKIE a concatenation of the entire cookie array as a single string?

If anyone else is wondering, here’s how you to it:

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !(^|\\s)some-cookie-name=some-cookie-value(;|$) [NC]
RewriteRule ^ /some-sort-of-error/ [NC,L,R=301]

Cookies are separated by space and ; so regex is used to check for newline and space on the left and semi colon on the right.