Set redirect code

This commit is contained in:
Jan-Lukas Else 2020-11-10 07:53:08 +01:00
parent 902f0f55e8
commit fa3242917b
2 changed files with 8 additions and 1 deletions

View File

@ -144,6 +144,7 @@ type configMicropubMedia struct {
type configRegexRedirect struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
Type int `mapstructure:"type"`
}
type configActivityPub struct {

View File

@ -10,6 +10,7 @@ var regexRedirects []*regexRedirect
type regexRedirect struct {
From *regexp.Regexp
To string
Type int
}
func initRegexRedirects() error {
@ -21,6 +22,7 @@ func initRegexRedirects() error {
regexRedirects = append(regexRedirects, &regexRedirect{
From: re,
To: cr.To,
Type: cr.Type,
})
}
return nil
@ -33,7 +35,11 @@ func checkRegexRedirects(next http.Handler) http.Handler {
newPath := re.From.ReplaceAllString(oldPath, re.To)
if oldPath != 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
}
}