1

I'm trying to get HAProxy to redirect to the right URI's in case the requested URI is non-www or non-https. The problem I'm encoutering is that it redirects as soon as it hits a line, resulting in multiple redirects.

This is my configuration:

*snip*

frontend traffic-in
    bind *:80
    bind *:443 ssl crt /etc/haproxy/certificate.bundled.pem
    reqrep ^(.*)[^/]$ \1/
    redirect prefix https://www.example.com 301 if { hdr(host) -i beste.nl } 
    redirect scheme https code 301 if !{ ssl_fc }

*snip*

If I would go to http://example.com, it would first redirect me to http://example.com/, followed by https://www.example.com.

I want it to redirect me once, not several times as this would hurt our Google ranking.

How can I redirect the request to a URI with the trailing slash AND ssl?

migrated from stackoverflow.com Jul 28 '15 at 8:35

This question came from our site for professional and enthusiast programmers.

1

If you split out a http frontend it might simplify things

frontend http_in
  bind *:80
  redirect prefix https://www.example.com code 301

frontend https_in
  all www.example.com hdr_sub(host) www.example.com
  bind *:443 ssl crt /etc/haproxy/certificate.bundled.pem
  redirect prefix https://www.example.com code 301 if !www.example.com
  ...
0

You need 3 frontend definition, like this:

frontend http
    bind *:80
    redirect scheme https code 301 if !{ ssl_fc }

frontend https
    bind *:443 ssl crt /etc/haproxy/certificate.bundled.pem
    #-------------------------
    # todo: use backends here
    #-------------------------

frontend www
    bind *:80
    acl has_www hdr_beg(host) -i www.
    http-request add-header X-Host-Redirect yes if !has_www
    redirect code 301 prefix //www.example.com if !has_www

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.