diff --git a/pkg/middlewares/replacepathregex/replace_path_regex.go b/pkg/middlewares/replacepathregex/replace_path_regex.go index fea6fa2de..fb2c47189 100644 --- a/pkg/middlewares/replacepathregex/replace_path_regex.go +++ b/pkg/middlewares/replacepathregex/replace_path_regex.go @@ -53,7 +53,7 @@ func (rp *replacePathRegex) ServeHTTP(rw http.ResponseWriter, req *http.Request) currentPath = req.URL.EscapedPath() } - if rp.regexp != nil && len(rp.replacement) > 0 && rp.regexp.MatchString(currentPath) { + if rp.regexp != nil && rp.regexp.MatchString(currentPath) { req.Header.Add(replacepath.ReplacedPathHeader, currentPath) req.URL.RawPath = rp.regexp.ReplaceAllString(currentPath, rp.replacement) diff --git a/pkg/middlewares/replacepathregex/replace_path_regex_test.go b/pkg/middlewares/replacepathregex/replace_path_regex_test.go index 59018d784..b7e637851 100644 --- a/pkg/middlewares/replacepathregex/replace_path_regex_test.go +++ b/pkg/middlewares/replacepathregex/replace_path_regex_test.go @@ -44,6 +44,28 @@ func TestReplacePathRegex(t *testing.T) { expectedRawPath: "/who-am-i/and/who-am-i", expectedHeader: "/whoami/and/whoami", }, + { + desc: "empty replacement", + path: "/whoami/and/whoami", + config: dynamic.ReplacePathRegex{ + Replacement: "", + Regex: `/whoami`, + }, + expectedPath: "/and", + expectedRawPath: "/and", + expectedHeader: "/whoami/and/whoami", + }, + { + desc: "empty trimmed replacement", + path: "/whoami/and/whoami", + config: dynamic.ReplacePathRegex{ + Replacement: " ", + Regex: `/whoami`, + }, + expectedPath: "/and", + expectedRawPath: "/and", + expectedHeader: "/whoami/and/whoami", + }, { desc: "no match", path: "/whoami/and/whoami",