mirror of https://github.com/jlelse/GoBlog
cache path redirects and improve regexredirects
This commit is contained in:
parent
9c0dd8155b
commit
dafcace8db
2
http.go
2
http.go
|
@ -343,7 +343,7 @@ func buildHandler() (http.Handler, error) {
|
||||||
r.Get("/robots.txt", serveRobotsTXT)
|
r.Get("/robots.txt", serveRobotsTXT)
|
||||||
|
|
||||||
// Check redirects, then serve 404
|
// Check redirects, then serve 404
|
||||||
r.With(checkRegexRedirects, cacheMiddleware, minifier.Middleware).NotFound(serve404)
|
r.With(cacheMiddleware, checkRegexRedirects, minifier.Middleware).NotFound(serve404)
|
||||||
|
|
||||||
r.With(minifier.Middleware).MethodNotAllowed(func(rw http.ResponseWriter, r *http.Request) {
|
r.With(minifier.Middleware).MethodNotAllowed(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
serveError(rw, r, "", http.StatusMethodNotAllowed)
|
serveError(rw, r, "", http.StatusMethodNotAllowed)
|
||||||
|
|
|
@ -19,27 +19,25 @@ func initRegexRedirects() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
regexRedirects = append(regexRedirects, ®exRedirect{
|
r := ®exRedirect{
|
||||||
From: re,
|
From: re,
|
||||||
To: cr.To,
|
To: cr.To,
|
||||||
Type: cr.Type,
|
Type: cr.Type,
|
||||||
})
|
}
|
||||||
|
if r.Type == 0 {
|
||||||
|
r.Type = http.StatusFound
|
||||||
|
}
|
||||||
|
regexRedirects = append(regexRedirects, r)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkRegexRedirects(next http.Handler) http.Handler {
|
func checkRegexRedirects(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
oldPath := r.URL.Path
|
|
||||||
for _, re := range regexRedirects {
|
for _, re := range regexRedirects {
|
||||||
newPath := re.From.ReplaceAllString(oldPath, re.To)
|
if newPath := re.From.ReplaceAllString(r.URL.Path, re.To); r.URL.Path != newPath {
|
||||||
if oldPath != newPath {
|
|
||||||
r.URL.Path = newPath
|
r.URL.Path = newPath
|
||||||
code := re.Type
|
http.Redirect(w, r, r.URL.String(), re.Type)
|
||||||
if code == 0 {
|
|
||||||
code = http.StatusFound
|
|
||||||
}
|
|
||||||
http.Redirect(w, r, r.URL.String(), code)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue