add header or query parameter when redirecting
Hello, I use the redirect directive to redirect users from old sites to a new site, eg: redirect prefix http://new-site.com code 301 if old-site I would like to redirect requests from many old sites to the same new site, so I need a way to add info about the old host in the new redirected request. I'm looking for a way to add a header to the redirected request to identify the host, for example : X-Orig-Site: old-site-123.com Is this possible ? I guess I can't add a header to the request with HAproxy, since HAproxy only sends a new Location header to the browser, and the browser sets the headers. So, is there a way to alter the location sent in the redirect, to include « orig-site=old-site-123.com » ? I think I'm missing something here. Should I user « http-request redirect » instead of « redirect prefix » ? By the way, I tried to use the set-cookie option for this, but it was a bad idea : redirect prefix http://new-site.com code 301 set-cookie ORIG=%[hdr(host)] if old_site This doesn't work for two reasons : 1. The « %[hdr(host)] » part is send literally in the request : Set-Cookie: ORIG=%[hdr(host)]; path=/; 2. The request sent to new-site.com doesn't seem to include this cookie Sylvain
Re: add header or query parameter when redirecting
On 06/03/2015 04:02 PM, Baptiste wrote: On Wed, Jun 3, 2015 at 11:58 AM, Sylvain Faivre sylvain.fai...@reservit.com wrote: Hello, I use the redirect directive to redirect users from old sites to a new site, eg: redirect prefix http://new-site.com code 301 if old-site I would like to redirect requests from many old sites to the same new site, so I need a way to add info about the old host in the new redirected request. I'm looking for a way to add a header to the redirected request to identify the host, for example : X-Orig-Site: old-site-123.com Is this possible ? I guess I can't add a header to the request with HAproxy, since HAproxy only sends a new Location header to the browser, and the browser sets the headers. So, is there a way to alter the location sent in the redirect, to include « orig-site=old-site-123.com » ? I think I'm missing something here. Should I user « http-request redirect » instead of « redirect prefix » ? By the way, I tried to use the set-cookie option for this, but it was a bad idea : redirect prefix http://new-site.com code 301 set-cookie ORIG=%[hdr(host)] if old_site This doesn't work for two reasons : 1. The « %[hdr(host)] » part is send literally in the request : Set-Cookie: ORIG=%[hdr(host)]; path=/; 2. The request sent to new-site.com doesn't seem to include this cookie Sylvain Hi Sylvain, The only good way to do what you want to achieve, is to use a query string parameter and http-request and http-response rules coupled to a few sections... Basically, haproxy is not able to modify the headers sent by a redirect rule. So the trick here, is to perform the redirect in a dummy frontend section used as a server in a dedicated backend and insert a header in the response, like this: backend be_redirect http-request capture req.hdr(host),word(1,:),lower len 32 http-response replace-value Location (.*) \1orig-site=%[capture.req.hdr(0)] if { res.hdr(Location) -m sub ? } http-response replace-value Location (.*) \1?orig-site=%[capture.req.hdr(0)] if !{ res.hdr(Location) -m sub ? } server dummy_redirect 127.0.0.1:8001 frontend fe_dummy_redirect bind 127.0.0.1:8001 http-request redirect prefix http://new-site.com code 301 Note that this configuration needs HAProxy 1.6 (latest snapshot). Baptiste Hi Baptiste, Unfortunately, we are not willing to upgrade to HAproxy 1.6 just yet, so we are going to use another solution for this redirect (change DNS records to resolve old hostnames to the new web server). Thank you for the info anyway, it may be useful for another time. Sylvain
Re: add header or query parameter when redirecting
On Wed, Jun 3, 2015 at 11:58 AM, Sylvain Faivre sylvain.fai...@reservit.com wrote: Hello, I use the redirect directive to redirect users from old sites to a new site, eg: redirect prefix http://new-site.com code 301 if old-site I would like to redirect requests from many old sites to the same new site, so I need a way to add info about the old host in the new redirected request. I'm looking for a way to add a header to the redirected request to identify the host, for example : X-Orig-Site: old-site-123.com Is this possible ? I guess I can't add a header to the request with HAproxy, since HAproxy only sends a new Location header to the browser, and the browser sets the headers. So, is there a way to alter the location sent in the redirect, to include « orig-site=old-site-123.com » ? I think I'm missing something here. Should I user « http-request redirect » instead of « redirect prefix » ? By the way, I tried to use the set-cookie option for this, but it was a bad idea : redirect prefix http://new-site.com code 301 set-cookie ORIG=%[hdr(host)] if old_site This doesn't work for two reasons : 1. The « %[hdr(host)] » part is send literally in the request : Set-Cookie: ORIG=%[hdr(host)]; path=/; 2. The request sent to new-site.com doesn't seem to include this cookie Sylvain Hi Sylvain, The only good way to do what you want to achieve, is to use a query string parameter and http-request and http-response rules coupled to a few sections... Basically, haproxy is not able to modify the headers sent by a redirect rule. So the trick here, is to perform the redirect in a dummy frontend section used as a server in a dedicated backend and insert a header in the response, like this: backend be_redirect http-request capture req.hdr(host),word(1,:),lower len 32 http-response replace-value Location (.*) \1orig-site=%[capture.req.hdr(0)] if { res.hdr(Location) -m sub ? } http-response replace-value Location (.*) \1?orig-site=%[capture.req.hdr(0)] if !{ res.hdr(Location) -m sub ? } server dummy_redirect 127.0.0.1:8001 frontend fe_dummy_redirect bind 127.0.0.1:8001 http-request redirect prefix http://new-site.com code 301 Note that this configuration needs HAProxy 1.6 (latest snapshot). Baptiste
Re: add header or query parameter when redirecting
Hi Baptiste, Unfortunately, we are not willing to upgrade to HAproxy 1.6 just yet, so we are going to use another solution for this redirect (change DNS records to resolve old hostnames to the new web server). Thank you for the info anyway, it may be useful for another time. Sylvain Well, HAPEE-1.5-r2 will have this feature and will be available soon. It's part of the backports from 1.6. Contact us at http://www.haproxy.com for more information. Cherry on the cake, you'll have access to our support team in the mean time :) Baptiste