大网数据广告活动
服务器运维问答

Haproxy change part of url

· 52CE 我爱测

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_nam…

问题原文

原帖链接 · Hugo Marques · CC BY-SA 3.0

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 · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。