From b7e16b5e5f5b6bacc94ecb62316f7a8cfaeb264b Mon Sep 17 00:00:00 2001 From: Catherine Date: Tue, 16 Sep 2025 16:51:11 +0000 Subject: [PATCH] Redirect from `/foo` to `/foo/` when serving `index.html` under it. Otherwise links break. --- src/pages.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pages.go b/src/pages.go index ad61b27..4334b42 100644 --- a/src/pages.go +++ b/src/pages.go @@ -44,8 +44,19 @@ func getPage(w http.ResponseWriter, r *http.Request) error { stat, statErr := file.Stat() if statErr == nil && stat.IsDir() { defer file.Close() - file, err = securejoin.OpenInRoot(config.DataDir, - filepath.Join(wwwRoot, requestPath, "index.html")) + if !strings.HasSuffix(r.URL.Path, "/") { + // redirect from `$root/$dir` to `$root/$dir/` or links in the document won't work + // correctly + newPath := r.URL.Path + "/" + w.Header().Set("Location", newPath) + w.WriteHeader(http.StatusFound) + fmt.Fprintf(w, "see %s\n", newPath) + return nil + } else { + // serve `$root/$dir/index.html` under `$root/$dir/` + file, err = securejoin.OpenInRoot(config.DataDir, + filepath.Join(wwwRoot, requestPath, "index.html")) + } } }