3

I am struggling with some HAproxy rules for a while with no luck unfortunately.

Let's say I have domain1.com and domain2.com. I want to redirect everything from domain1.com to domain2.com, including subdomains and parameters.

Ex:

  • foo.domain1.com => foo.domain2.com
  • foo.domain1.com/?bar => foo.domain2.com/?bar
  • domain1.com/?bar => domain2.com/?bar
  • etc

I've tried with reqrep ^([^\ ]*\) (.*)domain1.com(.*) 1/ domain2.com\2 but it does not do the trick.

Any ideas?

2 Answers2

1

Here's one way to to achieve that.

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 }

I think there's only one shortcoming in this solution: it seems to discard anchors from the URL. For example:

domain1.com?foo=bar#ze-anchor -> domain2.com?foo=bar
Henrique Zambon
  • 1,301
  • 1
  • 11
  • 20
-2

Put this in your frontend block:

redirect prefix http://domain2.com code 301 if { hdr(host) -i domain1.com }.

Same answer as this: https://stackoverflow.com/a/19826989/255523

Community
  • 1
  • 1
Ianthe the Duke of Nukem
  • 1,721
  • 2
  • 20
  • 37