diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-09-13 11:33:42 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-09-14 14:25:33 +0200 |
commit | a5cda5ca4dc9ced8179eb6bcccb1bbdc567afe17 (patch) | |
tree | a71f8bb6f4490ba65ff6ace20a205914de5e6350 /config | |
parent | 5e2b28d6e64cfb5d45ad557e1482b63e4ec84292 (diff) |
server: Add 404 support
Diffstat (limited to 'config')
-rw-r--r-- | config/commonConfig.go | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/config/commonConfig.go b/config/commonConfig.go index efaa589d1..31705841e 100644 --- a/config/commonConfig.go +++ b/config/commonConfig.go @@ -180,10 +180,15 @@ type Headers struct { } type Redirect struct { - From string - To string + From string + To string + + // HTTP status code to use for the redirect. + // A status code of 200 will trigger a URL rewrite. Status int - Force bool + + // Forcode redirect, even if original request path exists. + Force bool } func (r Redirect) IsZero() bool { @@ -200,16 +205,31 @@ func DecodeServer(cfg Provider) (*Server, error) { _ = mapstructure.WeakDecode(m, s) for i, redir := range s.Redirects { - // Get it in line with the Hugo server. - redir.To = strings.TrimSuffix(redir.To, "index.html") - if !strings.HasPrefix(redir.To, "https") && !strings.HasSuffix(redir.To, "/") { - // There are some tricky infinite loop situations when dealing - // when the target does not have a trailing slash. - // This can certainly be handled better, but not time for that now. - return nil, fmt.Errorf("unsupported redirect to value %q in server config; currently this must be either a remote destination or a local folder, e.g. \"/blog/\" or \"/blog/index.html\"", redir.To) + // Get it in line with the Hugo server for OK responses. + // We currently treat the 404 as a special case, they are always "ugly", so keep them as is. + if redir.Status != 404 { + redir.To = strings.TrimSuffix(redir.To, "index.html") + if !strings.HasPrefix(redir.To, "https") && !strings.HasSuffix(redir.To, "/") { + // There are some tricky infinite loop situations when dealing + // when the target does not have a trailing slash. + // This can certainly be handled better, but not time for that now. + return nil, fmt.Errorf("unsupported redirect to value %q in server config; currently this must be either a remote destination or a local folder, e.g. \"/blog/\" or \"/blog/index.html\"", redir.To) + } } s.Redirects[i] = redir } + if len(s.Redirects) == 0 { + // Set up a default redirect for 404s. + s.Redirects = []Redirect{ + { + From: "**", + To: "/404.html", + Status: 404, + }, + } + + } + return s, nil } |