Last active
January 19, 2023 02:23
-
-
Save meineerde/11865802f8338b18f123 to your computer and use it in GitHub Desktop.
HAPROXY: Redirect all requests to a URL starting with /foo to /bar while retaining everything following it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In HAProxy 1.5, we have to jump through some hops to accomplish a rewrite of a request's path... | |
# We use a temporary header to build our new path from the existing one in the request | |
# 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 full request URL into the X-REWRITE request header unchanged | |
http-request add-header X-REWRITE %[url] if { path_beg /foo } | |
# Change the X-REWRITE header to contain our new path | |
http-request replace-header X-REWRITE ^/foo(/.*)?$ /bar\1 if { hdr_cnt(X-REWRITE) gt 0 } | |
# Perform the 301 redirect | |
http-request redirect code 301 location http://%[hdr(host)]%[hdr(X-REWRITE)] if { hdr_cnt(X-REWRITE) gt 0 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In HAProxy 1.6, there is the regsub filter we can use here | |
http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/foo,/bar,)] if { path_beg /foo } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, but this produces ERR_TOO_MANY_REDIRECTS for me with HAProxy 1.5