Skip to content

Instantly share code, notes, and snippets.

@tonyyates
Last active June 7, 2018 09:47
Show Gist options
  • Save tonyyates/00e0578b2ffae25fe11127082e3cb978 to your computer and use it in GitHub Desktop.
Save tonyyates/00e0578b2ffae25fe11127082e3cb978 to your computer and use it in GitHub Desktop.
redirect prefix http://example.com code 301 if { hdr(host) -i www.example.com }
Please see the documentation of the redirect prefix rule for more information.
If you are using a newer version of HAProxy, i.e. at least 1.6, you can use a more generic syntax which allows to redirect any host, not just explicitly named
http-request redirect prefix http://%[hdr(host),regsub(^www\.,,i)] code 301 if { hdr_beg(host) -i www. }
Here, we are using the regsub filter to dynamically generate the correct hostname without the www. prefix.
In case you want to perform a redirect the other way around, i.e. to add a www if there is none already, the rule becomes simpler:
http-request redirect prefix http://www.%[hdr(host)] code 301 unless { hdr_beg(host) -i www. }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment