How to redirect URL with HAProxy
Asked Answered
N

1

25

I need redirect www.foo.com and foo.com to www.bar.com in haproxy, this is my configuration:

frontend  http-in
    bind *:80

    acl bar.com hdr(host) -i www.bar.com
    ...
    use_backend     bar.com_cluster if bar.com
    ... 
    redirect prefix http://foo.com code 301 if { hdr(host) -i www.bar.com }
    redirect prefix http://www.foo.com code 301 if { hdr(host) -i www.bar.com }
    ...

backend bar.com_cluster
    balance roundrobin
    option httpclose
    option forwardfor
    server bar 10.0.0.1:80 check

I have tried with redirect prefix but don't work, any idea?

Natachanatal answered 15/2, 2015 at 19:19 Comment(0)
N
41

Change order of the hostname:

redirect prefix http://www.bar.com code 301 if { hdr(host) -i foo.com }
redirect prefix http://www.bar.com code 301 if { hdr(host) -i www.foo.com }

instead of

redirect prefix http://foo.com code 301 if { hdr(host) -i www.bar.com }
redirect prefix http://www.foo.com code 301 if { hdr(host) -i www.bar.com }
Natachanatal answered 22/2, 2015 at 0:15 Comment(1)
Would you know how to do this with pfSense HAProxy? I tried Expression: Custom acl with no success.Sparkle

© 2022 - 2024 — McMap. All rights reserved.