Search code examples
httpshaproxyhttp-redirect

HAProxy redirect http://example.com and https://example.com to https://www.example.com


I have HAProxy configured to terminate SSL and send traffic to nginx instances. My HAProxy configuration is:

redirect scheme https code 301 if !letsenc !{ ssl_fc }
redirect scheme https code 301 if !{ ssl_fc }

Currently, traffic to http://example.com goes to https://example.com

I would like to redirect to www. subdomain as follows:

http://example.com -> https://www.example.com

https://example.com -> https://www.example.com

Can this be achieved with HAProxy without changing the nginx configuration?


Solution

  • You can use http-request redirect for such a redirect. This statement is similar to redirect location.

    # shameless copy from the doc
    http-request redirect code 301 location http://www.%[hdr(host)]%[capture.req.uri] unless { hdr_beg(host) -i www }