Search code examples
http-redirectweb-confighaproxy

HAProxy 2.5 http-request replace-path not working correctly


I need to have 301 redirect from example.com to www.example.com and from www.example.com/fr to www.example.com/fr/eshop

My haproxy.cfg:

global
    log stdout format raw local0

defaults
    log         global
    mode        http
    option      dontlognull
    option      http-ignore-probes
    log-format  "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
    option      forwardfor
    timeout     connect 30s
    timeout     client 30s
    timeout     server 30s

frontend http
    bind *:8080

    # redirect to www
    http-request redirect code 301 location https://www.example.com%[capture.req.uri] if { hdr(host) -i example.com }

    ### ACLs AND REDIRECTS ###
    acl path_de path_beg /de
    acl path_fr path_beg /fr
    acl path_fr_eshop path_beg /fr/eshop
    http-request replace-path /fr(.*) /fr/eshop\1 if path_fr !path_fr_eshop
    use_backend de if path_de
    use_backend fr if path_fr
    default_backend de

backend de
    http-request set-header Host www.example.com
    server de-gw 91.X.X.X:443 check ssl verify none

backend fr
    http-request set-header Host www.example.com
    server fr-gw 10.X.X.X:80

With this config redirect from non-www to www works fine

This also works fine: www.example.com/fr is getting redirected to www.example.com/fr/eshop

but www.example.com/fr/ is getting redirected to www.example.comfr/eshop/fr

I tried with following below replace-path:

http-request replace-path ^/fr(/|$)(.*) /fr/eshop\1\2 if path_fr !path_fr_eshop

but it did not help behavior is the same, /fr/ is getting redirected to /fr/eshop/fr


Solution

  • Evetything works OK with following replace-path:

    http-request replace-path /(.*) /fr/eshop if path_fr !path_fr_eshop