7

I have an app that was renamed and I want Haproxy to redirect to the correct path while preserving the request parameters

This is what I have:

acl old_name path_dir -i /old_name
   http-request set-path /new_name/%[query] if old_name

I want it to change from

www.site.com/old_name/Default.aspx?Id=123

to

www.site.com/new_name/Default.aspx?Id=123 but this is not working. 
Flint
  • 307
  • 2
  • 7
Hugo Marques
  • 73
  • 1
  • 1
  • 6
  • that this happen at IISM or MIT is not relevant and makes your question harder to understand. And what do you mean by "to have version control" ? – Flint Aug 30 '16 at 17:19
  • You're right, it's not really relevant to the question. By version control, I mean that you can actually see with a commit that you added a redirect for a url instead of being hidden in some config in IISM. – Hugo Marques Aug 31 '16 at 09:45

3 Answers3

12

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.

Per Lundberg
  • 173
  • 1
  • 9
Flint
  • 307
  • 2
  • 7
7

You are confusing url redirection with url rewriting to the backend.

Should you even want to rewrite, then according to the haproxy 1.6 documentation :

  • "set-path" rewrites the request path with the result of the evaluation of format string . The query string, if any, is left intact.

So the correct configuration in that case would be :

acl old_name path_dir -i /old_name
http-request set-path /new_name if old_name

To redirect the user :

redirect location /new_name if old_name
Flint
  • 307
  • 2
  • 7
  • 1
    Yeah I was a bit confused with those two. The thing is that I want redirect the user to /new_name/Default.aspx?Id=123 and keep the "Default.aspx?Id=123" part. Doing a redirect like you mentioned just redirects to /new_name instead of /new_name/Default.aspx?Id=123. – Hugo Marques Aug 31 '16 at 08:48
3

For anyone trying to make changes from uri to REST API, do not use the http-request redirect location because the header data is lost. Use http-request set-uri.

http-request set-uri %[url,regsub(^/old,/new,)] if { path_beg /old }
Ward - Trying Codidact
  • 12,899
  • 28
  • 46
  • 59
Daniel Sá
  • 31
  • 1