From 756f9df7e408aa6ae5de29cf5583adea97ba18a2 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Wed, 5 Jun 2019 21:33:57 +0300 Subject: [PATCH] For URLs without trailing slashes which resolve do a directory, do a redirect to add the trailing slash. --- httphandlers.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/httphandlers.go b/httphandlers.go index 45ee5fa..077962c 100644 --- a/httphandlers.go +++ b/httphandlers.go @@ -57,8 +57,15 @@ func GetHandler(config Config) http.HandlerFunc { http.Error(w, "Permission denied.", http.StatusForbidden) return } - // Add index.html to directory paths, if it exists + // Handle URLS which map to a directory if info.IsDir() { + // Redirect to add trailing slash if missing + // (otherwise relative links don't work properly) + if !strings.HasSuffix(r.URL.Path, "/") { + http.Redirect(w, r, r.URL.String()+"/", http.StatusMovedPermanently) + return + } + // Add index.html to directory paths, if it exists index_path := filepath.Join(path, "index.html") index_info, err := os.Stat(index_path) if !os.IsNotExist(err) {