Home > Software design >  Negative lookahead regex for apache mod header cookies - keep certain cookies
Negative lookahead regex for apache mod header cookies - keep certain cookies

Time:04-06

Trying to modify an apache request header directive, I need to use a negative lookahead regex to keep only certain cookies.

Test string

someCookie=someValue; anotherCookie=yada61; cookieToKeep1=myValue; cookieToKeep2=myValue2; lastCookie=yada1

Trying to remove all cookies but cookieToKeep1 and cookieToKeep2.

I can use the below regex but only matches the name and not the = and cookie value.

\\b((?!cookieToKeep1=\[^;\]*|cookieToKeep2=\[^;\]*).)\\S 

Apache directive that I tried:

RequestHeader edit Cookie "(\b((?!cookieToKeep1=[^;]*|cookieToKeep2=[^;]*).)\S )" ""

CodePudding user response:

You can use this directive to remove all cookies except cookieToKeep1 and cookieToKeep2:

RequestHeader edit* Cookie "\b(?!(?:cookieToKeep1|cookieToKeep2)=)([^;=] =[^;]*(?:; *|$))" ""

RequestHeader edit* performs multiple search and replacements.

RegEx Demo

  • Related