From f9b54454a19e2b070159ce8d2af76d819658244e Mon Sep 17 00:00:00 2001 From: diamondburned Date: Mon, 7 Jun 2021 11:20:08 -0700 Subject: fileserver: Redirect within the original URL (#4179) This commit changes the file_server directive to redirect using the original request's URL instead of the possibly trimmed URL. This should make file_server work with handle_path. This fix is taken from mholt's comment in https://caddy.community/t/file-servers-on-different-paths-not-working/11698/11. --- modules/caddyhttp/fileserver/browse.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'modules/caddyhttp/fileserver/browse.go') diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go index fc8bddb..444a656 100644 --- a/modules/caddyhttp/fileserver/browse.go +++ b/modules/caddyhttp/fileserver/browse.go @@ -47,10 +47,10 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, // URL doesn't end in a trailing slash because hrefs like // "/b/c" on a path like "/a" end up going to "/b/c" instead // of "/a/b/c" - so we have to redirect in this case - if !strings.HasSuffix(r.URL.Path, "/") { - fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path)) - r.URL.Path += "/" - http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) + oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request) + if !strings.HasSuffix(oldReq.URL.Path, "/") { + fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", oldReq.URL.Path)) + http.Redirect(w, r, oldReq.URL.Path+"/", http.StatusMovedPermanently) return nil } -- cgit v1.2.3