@marcel91200 / caddy Mirror · read-only / commits / 5823eccf
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 07:00 UTC

rewrite: Don't add / in Caddyfile, do it after replacer (#6662)

Francis Lavoie committed Nov 5, 2024 at 12:15 UTC 5823eccf99d9fc6b4ffba8a78acfdb59c4ec0c97
3 files changed +8 -3
modules/caddyhttp/rewrite/caddyfile.go
-3
@@ -110,9 +110,6 @@ func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, err
110 return nil, h.ArgErr()
111 }
112 rewr.StripPathPrefix = args[1]
113 - if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
114 - rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
115 - }
113
114 case "strip_suffix":
115 if len(args) != 2 {
modules/caddyhttp/rewrite/rewrite.go
+3
@@ -259,6 +259,9 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
259 // strip path prefix or suffix
260 if rewr.StripPathPrefix != "" {
261 prefix := repl.ReplaceAll(rewr.StripPathPrefix, "")
262 + if !strings.HasPrefix(prefix, "/") {
263 + prefix = "/" + prefix
264 + }
265 mergeSlashes := !strings.Contains(prefix, "//")
266 changePath(r, func(escapedPath string) string {
267 escapedPath = caddyhttp.CleanPath(escapedPath, mergeSlashes)
modules/caddyhttp/rewrite/rewrite_test.go
+5
@@ -235,6 +235,11 @@ func TestRewrite(t *testing.T) {
235 input: newRequest(t, "GET", "/prefix/foo/bar"),
236 expect: newRequest(t, "GET", "/foo/bar"),
237 },
238 + {
239 + rule: Rewrite{StripPathPrefix: "prefix"},
240 + input: newRequest(t, "GET", "/prefix/foo/bar"),
241 + expect: newRequest(t, "GET", "/foo/bar"),
242 + },
243 {
244 rule: Rewrite{StripPathPrefix: "/prefix"},
245 input: newRequest(t, "GET", "/prefix"),