3

I am working with HAProxy and want to be able to redirect all traffic from domainA to a second domainB preserving the prefix/subdomain.

I have tried things like:

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

which works but I may have several other domains each having sub-domains.

I want to be able to do something like

redirect prefix http://*.domainA.com code 301 if { hdr(host) -i http://*.domainB.com }
redirect prefix http://*.domainA.com code 301 if { hdr(host) -i http://*.domainC.org }

Domains B & C are effectively aliases for domainA in this instance. Is there a way of doing this so I only need on entry per alias domain?

To further increase complexity I would ideally want to ignore what protocol the inbound request comes in (http/https) and redirect to https

For this instance I would be looking at doing something like

redirect prefix http://*.domainA.com code 301 if { hdr(host) -i (http|https)://*.domainB.com }
redirect prefix http://*.domainA.com code 301 if { hdr(host) -i (http|https)://*.domainC.org }
SJC
  • 211
  • 2
  • 6

1 Answers1

3

I posted one solution for that in a different thread (Use haproxy to redirect .domain1.com/ to .domain2.com/ ).

acl domain_redirect hdr_end(host) -i domain1.com
http-request set-var(req.new_host) req.hdr(host),regsub(domain1\.com$,domain2.com,i) if domain_redirect
http-request redirect prefix https://%[var(req.new_host)] code 301 if { var(req.new_host) -m found }

As I mentioned there, this solution has at least one shortcoming, in that it seems to discard anchors from the URL. For example:

domain1.com?foo=bar#ze-anchor -> domain2.com?foo=bar