diff --git a/caddyhttp/staticfiles/fileserver.go b/caddyhttp/staticfiles/fileserver.go index d5cca1d3a..a66e1b136 100644 --- a/caddyhttp/staticfiles/fileserver.go +++ b/caddyhttp/staticfiles/fileserver.go @@ -106,8 +106,9 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request) (int, err // if an index file was explicitly requested, strip file name from the request // ("/foo/index.html" -> "/foo/") + var requestPage = path.Base(urlCopy.Path) for _, indexPage := range IndexPages { - if strings.HasSuffix(urlCopy.Path, indexPage) { + if requestPage == indexPage { urlCopy.Path = urlCopy.Path[:len(urlCopy.Path)-len(indexPage)] redir = true break diff --git a/caddyhttp/staticfiles/fileserver_test.go b/caddyhttp/staticfiles/fileserver_test.go index a71998a76..715915b3c 100644 --- a/caddyhttp/staticfiles/fileserver_test.go +++ b/caddyhttp/staticfiles/fileserver_test.go @@ -219,6 +219,13 @@ func TestServeHTTP(t *testing.T) { expectedLocation: "https://foo/bar/file1.html", expectedBodyContent: movedPermanently, }, + { + url: "https://foo/notindex.html", + expectedStatus: http.StatusOK, + expectedBodyContent: testFiles[webrootNotIndexHTML], + expectedEtag: `"2n9cm"`, + expectedContentLength: strconv.Itoa(len(testFiles[webrootNotIndexHTML])), + }, } for i, test := range tests { @@ -493,6 +500,7 @@ func TestServeHTTPFailingStat(t *testing.T) { // Paths for the fake site used temporarily during testing. var ( webrootFile1HTML = filepath.Join(webrootName, "file1.html") + webrootNotIndexHTML = filepath.Join(webrootName, "notindex.html") webrootDirFile2HTML = filepath.Join(webrootName, "dir", "file2.html") webrootDirHiddenHTML = filepath.Join(webrootName, "dir", "hidden.html") webrootDirwithindexIndeHTML = filepath.Join(webrootName, "dirwithindex", "index.html") @@ -519,6 +527,7 @@ var ( var testFiles = map[string]string{ "unreachable.html": "