For URLs without trailing slashes which resolve do a directory, do a redirect to add the trailing slash.

This commit is contained in:
Solderpunk 2019-06-05 21:33:57 +03:00
parent aad4ebba8f
commit 756f9df7e4
1 changed files with 8 additions and 1 deletions

View File

@ -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) {