Redirect legacy URL path

This commit is contained in:
Brian Picciano 2022-05-21 10:50:42 -06:00
parent 0361b1d4bf
commit e92f59fc7f

View File

@ -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
}