regexapachecookiesmod-headers

Apache mod-headers - keep only specific cookies


I am trying to use a "RequestHeader edit" directive to manipulate the "Cookie" header and only keep a specific set of cookies from that header.

RequestHeader edit Cookie "PATTERN TO REMOVE ALL COOKIES DESPITE OF .." ""

Incoming

someCookie=someValue; anotherCookie=yada61; cookieToKeep-1=myValue; cookieToKeep-2=myValue2; lastCookie=yada1

To keep

cookieToKeep-1=myValue; cookieToKeep-2=myValue2;

Goal is to remove all cookies but any cookie that starts with "cookieToKeep-".

I found that Pattern (CookieToKeep-\d=(([\w]*;)|[^\s]+)) gives me all matches for the cookies I need, but I need the negative of this pattern.


Solution

  • Try negative lookahead (for each cookie name): ^(?!cookieToKeep-).*.