From e92f59fc7ff48a65582fc03e192ac0b8d27aebec Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 21 May 2022 10:50:42 -0600 Subject: [PATCH] Redirect legacy URL path --- srv/src/http/index.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/srv/src/http/index.go b/srv/src/http/index.go index aa606d6..a392361 100644 --- a/srv/src/http/index.go +++ b/srv/src/http/index.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "path/filepath" + "regexp" "strings" "github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil" @@ -12,12 +13,24 @@ import ( func (a *api) renderIndexHandler() http.Handler { + legacyPostPathRegexp := regexp.MustCompile( + `^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([^/.]+)\.html$`, + ) + tpl := a.mustParseBasedTpl("index.html") const pageCount = 10 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - if path := r.URL.Path; !strings.HasSuffix(path, "/") && filepath.Base(path) != "index.html" { + path := r.URL.Path + + if matches := legacyPostPathRegexp.FindStringSubmatch(path); len(matches) == 2 { + id := matches[1] + http.Redirect(rw, r, filepath.Join("/posts", id), http.StatusMovedPermanently) + return + } + + if !strings.HasSuffix(path, "/") && filepath.Base(path) != "index.html" { http.Error(rw, "Page not found", 404) return }