Browse Source

Set redirect code

master
Jan-Lukas Else 2 months ago
parent
commit
fa3242917b
2 changed files with 8 additions and 1 deletions
  1. +1
    -0
      config.go
  2. +7
    -1
      regexRedirects.go

+ 1
- 0
config.go 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 {


+ 7
- 1
regexRedirects.go 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
}
}


Loading…
Cancel
Save