extract string from req.url.path in VCL Fastly

1.7k views Asked by At

I need help to extract values from string req.url.path which looks like this:

/a/b/c/d

Need to extract c, if there is url, like /a, it should return ''. I tried

regsub(req.url.path, "/a/b/", "\5");

Tried with replace too. but it is not effectively working for me. Please help me.

1

There are 1 answers

0
Integralist On

I believe the following might be along the lines of what you require:

if (req.url.path ~ "^/a/.+/c") {
  set req.url = regsub(req.url.path, "/c", "") + "?" + req.url.qs;
}

It checks if the URL path begins with /a and is followed by /c (presuming also that there are segments in-between, such as /b as per your given example /a/b/c/d).

Here is a Fastly Fiddle link for you to play around with the code:
https://fiddle.fastlydemo.net/fiddle/527480ba

So given the input URL path:

/a/b/c/d?id=testing

It would change to:

/a/b/d?id=testing

Notice that the /c has been extracted from the path.