From ab653f7555495d674104d1d41ba4a79bc55fd104 Mon Sep 17 00:00:00 2001 From: thinkpadmaster Date: Fri, 21 Jul 2023 22:41:35 -0500 Subject: Handle trailing slashes and extraneous URL Paths for /about, /projects, and /blog --- handle.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/handle.go b/handle.go index 5059c1d..5b84d4e 100644 --- a/handle.go +++ b/handle.go @@ -26,9 +26,15 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { } func (app *application) about(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/about/" { + path := strings.Split(r.URL.Path, "/") + pathLen := len(path) + + if pathLen == 3 && path[2] == "" { http.Redirect(w, r, "/about", http.StatusFound) return + } else if pathLen == 3 && path[2] != "" || pathLen > 3 { + app.notFound(w) + return } err := renderTemplate(w, "main/about", nil) if err != nil { @@ -48,11 +54,15 @@ func (app *application) aggregate(w http.ResponseWriter, r *http.Request) { func (app *application) post(w http.ResponseWriter, r *http.Request) { path := strings.Split(r.URL.Path, "/") - if r.URL.Path == "/blog" || r.URL.Path == "/projects" { + if len(path) > 4 { + app.notFound(w) + } else if r.URL.Path == "/blog" || r.URL.Path == "/projects" { app.aggregate(w, r) } else if path[2] == "" { - http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusFound) + http.Redirect(w, r, "/"+path[1], http.StatusFound) return + } else if len(path) == 4 && path[3] == "" { + http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound) } else { post, err := app.readFile("html" + strings.TrimSuffix(r.URL.Path, "/") + ".tmpl.html") if err != nil { -- cgit v1.2.3