With HAProxy 1.5 : use a temporary header to build a 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 X-Rewrite unchanged
http-request add-header X-REWRITE %[url] if { path_beg /old_path }
# Change the X-REWRITE header to contain out new path
http-request replace-header X-REWRITE ^/old_path(/.*)?$ /new_path\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 }
In HAProxy 1.6, use the regsub filter
http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/old_path,/new_path,)] if { path_beg /old_path }
source among other useful configuration snippets
More information is available in the HAProxy documentation for the regsub keyword.