diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-01-11 13:47:42 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-01-11 13:47:42 -0700 |
commit | 25dea2903e613534e00a2a4e305e0c8f2daeea8d (patch) | |
tree | a1dabd4ffa5a277adea31ee309f246be0baf6ff7 /modules/caddyhttp/fileserver | |
parent | d876de61e512db7a31a7ae59723d5134048f283e (diff) |
http: A little more polish on rewrite handler and try_files directive
Diffstat (limited to 'modules/caddyhttp/fileserver')
-rw-r--r-- | modules/caddyhttp/fileserver/caddyfile.go | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/modules/caddyhttp/fileserver/caddyfile.go b/modules/caddyhttp/fileserver/caddyfile.go index aa11ba0..d26b435 100644 --- a/modules/caddyhttp/fileserver/caddyfile.go +++ b/modules/caddyhttp/fileserver/caddyfile.go @@ -107,11 +107,17 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // try_files <files...> // } // } -// rewrite @try_files {http.matchers.file.relative}{http.request.uri.query_string} +// rewrite @try_files {http.matchers.file.relative} // -// If any of the files in the list have a query string, the query string will -// be ignored when checking for file existence, but will be augmented into -// the request's URI when rewriting the request. +// This directive rewrites request paths only, preserving any other part +// of the URI, unless the part is explicitly given in the file list. For +// example, if any of the files in the list have a query string: +// +// try_files {path} index.php?{query}&p={path} +// +// then the query string will not be treated as part of the file name; and +// if that file matches, the given query string will replace any query string +// that already exists on the request URI. func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) { if !h.Next() { return nil, h.ArgErr() @@ -123,16 +129,14 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) } // makeRoute returns a route that tries the files listed in try - // and then rewrites to the matched file, and appends writeURIAppend - // to the end of the query string. - makeRoute := func(try []string, writeURIAppend string) []httpcaddyfile.ConfigValue { + // and then rewrites to the matched file; userQueryString is + // appended to the rewrite rule. + makeRoute := func(try []string, userQueryString string) []httpcaddyfile.ConfigValue { handler := rewrite.Rewrite{ - URI: "{http.matchers.file.relative}{http.request.uri.query_string}" + writeURIAppend, + URI: "{http.matchers.file.relative}" + userQueryString, } matcherSet := caddy.ModuleMap{ - "file": h.JSON(MatchFile{ - TryFiles: try, - }), + "file": h.JSON(MatchFile{TryFiles: try}), } return h.NewRoute(matcherSet, handler) } @@ -149,7 +153,7 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) result = append(result, makeRoute(try, "")...) try = []string{} } - result = append(result, makeRoute([]string{item[:idx]}, "&"+item[idx+1:])...) + result = append(result, makeRoute([]string{item[:idx]}, item[idx:])...) continue } // accumulate consecutive non-query-string parameters |