Replace path clarification

I’m attempting to create some sort of virtual directory that exists at any level, even if nonexistent path or the domain name itself. I need to keep the last two segments of the path, one known, one unknown, get rid of the rest, pick the backend and I’m done. I’ve already found the right keyword (http-request replace-path) but it’s not clear if it will replace only that matched regex, or the whole string. That’s why I’m here. :nerd_face:

For instance, I need something like:

IF REQUEST IS                                                                  REWRITE AS
<sch><dom1>:<prt>/wiki/statcap/404.svg         → http://resourceserver:80/statcap/404.svg
<sch><dom1>:<prt>/statcap/404.svg              → http://resourceserver:80/statcap/404.svg
<sch><dom2>:<prt>/callme/mrs/statcap/404.svg   → http://resourceserver:80/statcap/404.svg

So I came up with <1> if everything is replaced regardless of how much is matched, with <2a> or <2b> if only what’s matched gets replaced and I was going to use <3> to filter it down as advised (it’s not taken into consideration for the match, right?)

 1. '  http-request replace-path (\/statcap\/)(.*)$ \1\2        '
2a. '  http-request replace-path (\/.+)(\/statcap\/)(.*)$ \2\3  '
2b. '  http-request replace-path \/.+(\/statcap\/)(.*)$ \1\2    '
 3. '  { var(txn.txnpath) -m dir -i statcap }                   '

Would it work? Am I even close?

Also, could you clarify if "/"s need to be escaped? In the docu they aren’t. But then again “\” not “$” is used for backreferences. Also not mentioned is case matching, with regsub you’d use commas in I think it was parenthesis or sq brackets, something like regsub(<regex>,<replace>,ig), I think. But here I didn’t get anything of that, it’s a little misplaced, it builds on http-request replace-header which itself only mentions it’s case-sensitive and that’s it.

I solved it! :smiley:

It worked with the first one, I also had to add a new line because the resource server hosts multiple sites though. If anyone needs is looking for an example of this in the future, here’s the code:

acl statcapmatch var(txn.txnpath) -m dir -i statcap
http-request replace-path (\/statcap\/)(.*)$ \1\2 if statcapmatch
http-request set-header Host status.domain.tld if statcapmatch

I also moved from dual anonymous ACLs to a single regular ACL, it seemed more efficient; that’s just my speculation though.

Thanks anyway! :smiley: