From d242f10eda8fdd52f86701cf3685090e9761489f Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 27 Aug 2019 14:38:24 -0600 Subject: [PATCH] Add query_string to HTTP replacer and use it for try_files --- modules/caddyhttp/fileserver/caddyfile.go | 4 ++-- modules/caddyhttp/replacer.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/fileserver/caddyfile.go b/modules/caddyhttp/fileserver/caddyfile.go index 7afcc9ef..6480598b 100644 --- a/modules/caddyhttp/fileserver/caddyfile.go +++ b/modules/caddyhttp/fileserver/caddyfile.go @@ -17,9 +17,9 @@ package fileserver import ( "encoding/json" - "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite" "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" "github.com/caddyserver/caddy/v2/modules/caddyhttp" + "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite" ) func init() { @@ -99,7 +99,7 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) } handler := rewrite.Rewrite{ - URI: "{http.matchers.file.relative}{http.request.uri.query}", + URI: "{http.matchers.file.relative}{http.request.uri.query_string}", } matcherSet := map[string]json.RawMessage{ diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index cc29789b..9a8a875e 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -89,6 +89,12 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon return dir, true case "http.request.uri.query": return req.URL.RawQuery, true + case "http.request.uri.query_string": + qs := req.URL.Query().Encode() + if qs != "" { + qs = "?" + qs + } + return qs, true } // hostname labels