HAProxy Redirects and matching based off URI

Solution 1:

Let me suggest a few things:

  • You should separate your insecure and secure config into two separate blocks, this way you can more easily control the ACL and redirect cases.
  • For the redirect try using the format redirect location <absolute_url> if <conditions>
  • For detecting URI paths, try using path_beg -i /blog
  • Finally you're not going to be able to actually redirect people away from an HTTPS connection, this is considered insecure and is not supported.

Here are my suggested modifications to the incoming portion of your config, based on these comments. This should redirect http://www.test.com to https://www.test.com AND will fail when an attempt is made to https://www.test.com/blog (you could put a page there with a link suggestion for the user).

    frontend public
      bind *:80
      acl has_blog_uri path_beg -i /blog
      redirect location https://www.test.com if !has_blog_uri
      use_backend blog_app if has_blog_uri    

    frontend public-ssl
      bind *:443 ssl crt /etc/haproxy/test.pem
      acl has_blog_uri path_beg -i /blog
      use_backend rails_app if !has_blog_uri

Hope this helps.