Skip to content

Instantly share code, notes, and snippets.

@sinfere
Forked from troyfontaine/beg_redirect.map
Created November 16, 2022 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sinfere/afd8a16647b4ba07f65f55d5472dd365 to your computer and use it in GitHub Desktop.
Save sinfere/afd8a16647b4ba07f65f55d5472dd365 to your computer and use it in GitHub Desktop.
HAProxy Redirecting based on an HTTP Query or a Map with a fallback redirect based on host header
# This file contains only partial paths to match on
# This file should reside in the same directory as the haproxy.cfg simply due to the way it is configured in the sample snippet-but they can go anywhere in the /etc/haproxy directory
/my-partial- https://subdomain3.mydomain.com/my-new-full-path
# ACL for matching on a hostname
acl target_host hdr(host) -i subdomain2.mydomain.com
# ACL for matching on a query parameter (e.g. ?foo=bar)
acl my_query_param query queryname=value
# Match on partial path in the map that includes the query
http-request redirect location %[path,map_beg(beg_redirect.map)]?%[query] code 301 if { path,map_beg(beg_redirect.map) -m found } my_query_param target_host
# Match on partial path
http-request redirect location %[path,map_beg(beg_redirect.map)] code 301 if { path,map_beg(beg_redirect.map) -m found } target_host
# Match on full path that includes the query
http-request redirect location %[capture.req.uri,map(redirect.map)]?%[query] code 301 if { capture.req.uri,map(redirect.map) -m found } my_query_param target_host
# Match on full path
http-request redirect location %[capture.req.uri,map(redirect.map)] code 301 if { capture.req.uri,map(redirect.map) -m found } target_host
# General catch all if none of the previous rules have matched
http-request redirect location https://subdomain3.mydomain.com code 301 if target_host
# This file contains full paths to redirect
# This file should reside in the same directory as the haproxy.cfg simply due to the way it is configured in the sample snippet-but they can go anywhere in the /etc/haproxy directory
/my-old-path https://subdomain3.mydomain.com/my-new-path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment