diff options
author | thinkpadmaster <a.scerba02@gmail.com> | 2023-07-21 22:41:35 -0500 |
---|---|---|
committer | Alex Scerba <alex@scerba.org> | 2024-10-29 13:19:57 -0400 |
commit | ab653f7555495d674104d1d41ba4a79bc55fd104 (patch) | |
tree | 45c4b735ddfe460f1ce62631c157092f2e7ba7eb | |
parent | fa4169e37b6d913cb3fd3e79ae1f00459399eccb (diff) |
Handle trailing slashes and extraneous URL Paths for /about, /projects, and /blog
-rw-r--r-- | handle.go | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -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 { |