Search code examples
haproxy

HA Proxy rewrite and redirect


Trying to do a rewrite and redirect. I've been trying this, it works to some extent but not 100% what I want it to do

acl old url_beg /site/ab http-request redirect location /new/%[query] if old

the url can be for example

  • https://host/site/ab/xx

  • https://host/site/ab/yyyy

  • https://host/site/ab/zzzzzz

  • https://host/site/ab/zzzzzz/asdajshdjasd

I am looking to grab the bold marked part and simply redirect the user to https//host/new/boldmarkedpart

Any string that comes after the bold marked part can be trashed. For example "/asdajshdjasd" in the last example.

Any idea how to accomplish this? Thank you!!


Solution

  • If i understand correctly, you want to split the path part of url and get its 4th part. In string foo1/foo2/foo3/foo4/foo5 you want only foo4. This should work for you:

    acl old path_beg /site/ab
    http-request redirect location /new/%[path,field(4,/)] if old
    

    It may be confusing that you want 3rd directory from path and here you take 4th word, but that's because when you split /foo2/foo3/foo4/foo5 by / then the first word is empty. field converter is documented here: https://cbonte.github.io/haproxy-dconv/2.2/configuration.html#7.3.1-field

    Other notes:

    • %[query] would return the query part of url, which is everything after ? character and you don't have query part at all in your examples.
    • url in my tests had schema://hostname:port/path, so testing acl old url_beg /site/ab never matched, path is for that