0

I have several domains which are variations of my main domain. I want to redirect them all to my main domain ignoring the protocol (http/https) and subdomain (www./beta.). Is there a way to do this in HAProxy config?

I was thinking of something like

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

1 Answers1

1

Redirecting *.example.org to https://*.example.com, preserving the subdomain, path, and query string if present (linebreaks added for readability)...

http-request redirect 
  location 
  https://%[hdr(host),lower,regsub(\.example\.org$,.example.com,)]%[capture.req.uri] 
  code 301
  if { hdr_end(host) -i .example.org  }

Take the incoming host header, convert to lowercase, then do a regex replacement of .example.org anchored to the end, changing to .example.com, then append the captured request URI, and the code to 301, if the Host header ends in .example.org, case-insensitive.

Test...

$ curl -v http://longcat.example.org/test?cat=1
* Connected to longcat.example.org (127.0.0.1) port 80 (#0)
> GET /test?cat=1 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: longcat.example.org
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://longcat.example.com/test?cat=1
< Connection: close

Test with mixed case host header...

$ curl -v http://TACGNOL.eXaMpLe.ORG/ohai?cat=2
* Connected to TACGNOL.eXaMpLe.ORG (127.0.0.1) port 80 (#0)
> GET /ohai?cat=2 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: TACGNOL.eXaMpLe.ORG
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://tacgnol.example.com/ohai?cat=2
< Connection: close

This solution relies on features first introduced in HAProxy 1.6.

Repeat the line with the appropriate edits for each pair of domains.

Michael - sqlbot
  • 22,658
  • 2
  • 63
  • 86