diff --git a/Makefile b/Makefile index d462fe27..8e197545 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,11 @@ GO_LDFLAGS ?= "-X=$(VERSYM)=$(VERSION) -X=$(GITSHASYM)=$(GITSHA) -X=$(BUILDOSSYM # gateway-api GATEAY_API_VERSION ?= v1.3.0 ## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/pkg/features/httproute.go +<<<<<<< HEAD SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080" +======= +SUPPORTED_EXTENDED_FEATURES = "HTTPRouteDestinationPortMatching,HTTPRouteMethodMatching,HTTPRoutePortRedirect,HTTPRouteRequestMirror,HTTPRouteSchemeRedirect,GatewayAddressEmpty,HTTPRouteResponseHeaderModification,GatewayPort8080,HTTPRouteHostRewrite,HTTPRouteQueryParamMatching,HTTPRoutePathRewrite" +>>>>>>> 15132023 (fix(conformance-test): HTTPRoutePathRewrite (#2597)) CONFORMANCE_TEST_REPORT_OUTPUT ?= $(DIR)/apisix-ingress-controller-conformance-report.yaml ## https://github.com/kubernetes-sigs/gateway-api/blob/v1.3.0/conformance/utils/suite/profiles.go CONFORMANCE_PROFILES ?= GATEWAY-HTTP,GATEWAY-GRPC diff --git a/internal/adc/translator/httproute.go b/internal/adc/translator/httproute.go index ddb5d329..e0559ace 100644 --- a/internal/adc/translator/httproute.go +++ b/internal/adc/translator/httproute.go @@ -121,14 +121,33 @@ func (t *Translator) fillPluginFromURLRewriteFilter(plugins adctypes.Plugins, ur } prefixPaths = append(prefixPaths, *match.Path.Value) } - regexPattern := "^(" + strings.Join(prefixPaths, "|") + ")" + "/(.*)" + if len(prefixPaths) == 0 || urlRewrite.Path.ReplacePrefixMatch == nil { + break + } + prefixGroup := "(" + strings.Join(prefixPaths, "|") + ")" replaceTarget := *urlRewrite.Path.ReplacePrefixMatch - regexTarget := replaceTarget + "/$2" - - plugin.RewriteTargetRegex = []string{ - regexPattern, - regexTarget, + // Handle ReplacePrefixMatch path rewrite + // If replaceTarget == "/", special handling is required to avoid + // producing double slashes or empty paths. + var regexPattern, regexTarget string + if replaceTarget == "/" { + // Match either "/prefix" or "/prefix/" + // Pattern captures the remainder (if any) without a leading slash. + // Template reconstructs "/" + remainder, resulting in: + // /prefix/three → /three + // /prefix → / + regexPattern = "^" + prefixGroup + "(?:/(.*))?$" + regexTarget = "/" + "$2" + } else { + // Match either "/prefix" or "/prefix/" + // Pattern captures the remainder (including leading slash) as $2. + // Template appends it to replaceTarget: + // /prefix/one/two → /one/two + // /prefix/one → /one + regexPattern = "^" + prefixGroup + "(/.*)?$" + regexTarget = replaceTarget + "$2" } + plugin.RewriteTargetRegex = []string{regexPattern, regexTarget} } } }