使用在Ubuntu 12.04上运行的Haproxy 1.5.12
我的网站得到很多像这样的请求:
http://www.example.com/foo/bar/mypage.php?gallery=&position=3
正确的URL应该是:
http://www.example.com/foo/bar/mypage.php?gallery=photogallery&position=3
我已经成功地将请求重写到正确的URL,但我也想发出301redirect到客户端。
以下这篇文章: 使用haproxyredirect重写的URL我试过:
acl fix_gallery url_sub gallery=& reqrep (.*)gallery=&(.*) \1gallery=photogallery&\2 redirect prefix / code 301 if fix_gallery
试图有创意我试过了:
acl fix_gallery url_reg (.*)gallery=&(.*) acl fix_gallery urlp_reg(gallery) -m str "" acl fix_gallery urlp_reg(gallery) -m len 0
还有很多。 但似乎没有任何工作,所以我显然失去了一些东西。
有什么build议么?
谢谢
您可以使用3行configuration来利用http-request关键字来实现您要查找的内容。
第一个设置一个我们将在以下两个中使用的虚拟标题。
http-request set-header X-Location-Path %[capture.req.uri] if fix_gallery
第二个执行您需要修复URL查询的replace。
http-request replace-header X-Location-Path (.*)gallery=&(.*) \1gallery=photogallery&\2 if fix_gallery
最后一行是改变URL的方向。
http-request redirect location http://www.example.com/%[hdr(X-Location-Path)] if fix_gallery
如果你只有一个域,这可以工作,但是可以构buildhttp-request redirect行,可以和任何域和scheme一起工作。
http-request redirect location https://%[hdr(host)]/%[hdr(X-Location-Path)] if fix_gallery { ssl_fc } http-request redirect location http://%[hdr(host)]/%[hdr(X-Location-Path)] if fix_gallery !{ ssl_fc }