HAProxy community

Need to remove 1st part of URL

I have to redirect my URL to the following address and then remove the 1st part of the URL and then send it to the backend.
The absolute URL is: https://test.domain.com
It should redirect to the: https://test.domain.com/portal
then I should remove the first part (Portal I mean) and then add the next parts (after /portal) to the URL and send it to the backend.
**the final URL which must send to the backend should be something like this: https://test.domain.com/dist/… **
The HAProxy version is: HA-Proxy version 1.8.4-1deb90d 2018/02/08
Now I have the following configuration:

frontend MAIN-HTTPS
bind :443 ssl transparent crt /etc/ssl/certs
acl test hdr(host) test.domain.com
reqirep ^([^\ :]
)\ /(.*) \1\ /portal/\2 if test
use_backend testPortal if test

backend testPortal
mode http
acl no_redir url_beg /portal
reqrep ^([^\ :])\ /(.) \1\ /portal/\2 if no_redir
http-request redirect scheme https if !{ ssl_fc }
server web-testPortal test.domain.com:100 check

please help me!

I would suggest using regsub fetch transform in the backend, something similar to:

http-request set-path "%[path,regsub(^/portal/,/)]"
1 Like

Thanks Ciprian
I used regsub exactly as you mentioned but the /portal part is still there!
my backend configuration is as below

backend testPortal
mode http
http-request set-path “%[path,regsub(^/portal/,/)]”
server web-testPortal test.domain.com:100 check

do you have any recommendation?

What URL’s are you trying?

For example:

  • /portal/whatever -> /whatever – should work;
  • /portal/ -> / – should also work;
  • /portalshouldn’t work as it’s missing the last /;

You should adapt the regular expression as needed, like for example: ^/portal(/|$). (Please note that the HAProxy syntax does impose some limitations, therefore you need to experiment a little…)

1 Like

Dear Ciprian,

Thank you very much. I was wrong and your solution worked like a charm.