How to insert a variable in Varnish regex

174 views Asked by At

I have a variable I'd like to include in Varnish regex match, but having trouble finding the correct syntax, example below. Appreciate any tips.

if (req.url !~ "slreturn=(req.http.yearMonthDay)") {...

2

There are 2 answers

4
Thijs Feryn On

@Kari I see that you're using a mixture of double quotes & single quotes to interpolate the req.http.YearMonthDay header into the regex.

Please use double quotes only as illustrated below:

if (req.url ~ "slreturn=" && req.url ~ "[?:"+req.http.yearMonthDay+"]" ) {
  std.log("------> YOU MATCHED. GO FORTH AND DO STUFF ");
}

If that still doesn't work for you, please paste the error that the Varnish compiler returns when starting Varnish.

0
Kari On

I changed my thinking, using std.strstr made a huge difference.

if (std.strstr(req.url, req.http.yearMonthDay)) {
  // Do something based on the match
  std.log("------> YOU MATCHED TO TODAY. WHAT DO YOU WANT TO DO NOW? ");
}