mirror of https://github.com/jlelse/GoBlog
Set redirect code
This commit is contained in:
parent
902f0f55e8
commit
fa3242917b
|
@ -144,6 +144,7 @@ type configMicropubMedia struct {
|
||||||
type configRegexRedirect struct {
|
type configRegexRedirect struct {
|
||||||
From string `mapstructure:"from"`
|
From string `mapstructure:"from"`
|
||||||
To string `mapstructure:"to"`
|
To string `mapstructure:"to"`
|
||||||
|
Type int `mapstructure:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type configActivityPub struct {
|
type configActivityPub struct {
|
||||||
|
|
|
@ -10,6 +10,7 @@ var regexRedirects []*regexRedirect
|
||||||
type regexRedirect struct {
|
type regexRedirect struct {
|
||||||
From *regexp.Regexp
|
From *regexp.Regexp
|
||||||
To string
|
To string
|
||||||
|
Type int
|
||||||
}
|
}
|
||||||
|
|
||||||
func initRegexRedirects() error {
|
func initRegexRedirects() error {
|
||||||
|
@ -21,6 +22,7 @@ func initRegexRedirects() error {
|
||||||
regexRedirects = append(regexRedirects, ®exRedirect{
|
regexRedirects = append(regexRedirects, ®exRedirect{
|
||||||
From: re,
|
From: re,
|
||||||
To: cr.To,
|
To: cr.To,
|
||||||
|
Type: cr.Type,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -33,7 +35,11 @@ func checkRegexRedirects(next http.Handler) http.Handler {
|
||||||
newPath := re.From.ReplaceAllString(oldPath, re.To)
|
newPath := re.From.ReplaceAllString(oldPath, re.To)
|
||||||
if oldPath != newPath {
|
if oldPath != newPath {
|
||||||
r.URL.Path = newPath
|
r.URL.Path = newPath
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusFound)
|
code := re.Type
|
||||||
|
if code == 0 {
|
||||||
|
code = http.StatusFound
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, r.URL.String(), code)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue