Skip to content

Commit 5823ecc

Browse files
rewrite: Don't add / in Caddyfile, do it after replacer (#6662)
1 parent cc23ad6 commit 5823ecc

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

modules/caddyhttp/rewrite/caddyfile.go

-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, err
110110
return nil, h.ArgErr()
111111
}
112112
rewr.StripPathPrefix = args[1]
113-
if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
114-
rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
115-
}
116113

117114
case "strip_suffix":
118115
if len(args) != 2 {

modules/caddyhttp/rewrite/rewrite.go

+3
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
259259
// strip path prefix or suffix
260260
if rewr.StripPathPrefix != "" {
261261
prefix := repl.ReplaceAll(rewr.StripPathPrefix, "")
262+
if !strings.HasPrefix(prefix, "/") {
263+
prefix = "/" + prefix
264+
}
262265
mergeSlashes := !strings.Contains(prefix, "//")
263266
changePath(r, func(escapedPath string) string {
264267
escapedPath = caddyhttp.CleanPath(escapedPath, mergeSlashes)

modules/caddyhttp/rewrite/rewrite_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func TestRewrite(t *testing.T) {
235235
input: newRequest(t, "GET", "/prefix/foo/bar"),
236236
expect: newRequest(t, "GET", "/foo/bar"),
237237
},
238+
{
239+
rule: Rewrite{StripPathPrefix: "prefix"},
240+
input: newRequest(t, "GET", "/prefix/foo/bar"),
241+
expect: newRequest(t, "GET", "/foo/bar"),
242+
},
238243
{
239244
rule: Rewrite{StripPathPrefix: "/prefix"},
240245
input: newRequest(t, "GET", "/prefix"),

0 commit comments

Comments
 (0)