Skip to content

Instantly share code, notes, and snippets.

@meineerde
Last active January 25, 2018 00:00
Show Gist options
  • Save meineerde/1346953c12f843425694 to your computer and use it in GitHub Desktop.
Save meineerde/1346953c12f843425694 to your computer and use it in GitHub Desktop.
Redirect *.example.com to *.example.org with HAProxy
# We use a temporary header to build our new Host header from the existing one
# and then directly perform a redirect
# Clean the request and remove any existing header named X-Rewrite
http-request del-header X-REWRITE
# Copy the Host header into X-Rewrite unchanged
http-request add-header X-REWRITE %[hdr(host)] if { hdr_end(host) .example.com }
# Change the X-REWRITE Header to contain out new host name
http-request replace-header X-REWRITE (.*)\.example\.com$ \1.example.org if { hdr_count(X-REWRITE) gt 0 }
# Perform the 301 redirect
http-request redirect code 301 location %[hdr(X-REWRITE)]%[req.uri] if { hdr_count(X-REWRITE) gt 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment