summaryrefslogtreecommitdiffhomepage
path: root/commands
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2020-10-05 17:56:28 +0200
committerBjørn Erik Pedersen <[email protected]>2020-10-05 22:20:00 +0200
commit5e2a547cb594b31ecb0f089b08db2e15c6dc381a (patch)
treeb3b529ca40bc6ac2335c2c2bde782e1c964b8d63 /commands
parentee090c0940cdbf636e3a55a40b41612d92b9c62d (diff)
downloadhugo-5e2a547cb594b31ecb0f089b08db2e15c6dc381a.tar.gz
hugo-5e2a547cb594b31ecb0f089b08db2e15c6dc381a.zip
Add force flag to server redirects config
Fixes #7778
Diffstat (limited to 'commands')
-rw-r--r--commands/server.go36
1 files changed, 27 insertions, 9 deletions
diff --git a/commands/server.go b/commands/server.go
index 602527253..7d604b97d 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -376,17 +376,36 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
}
if redirect := f.c.serverConfig.MatchRedirect(requestURI); !redirect.IsZero() {
+ doRedirect := true
// This matches Netlify's behaviour and is needed for SPA behaviour.
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/
- if redirect.Status == 200 {
- if r2 := f.rewriteRequest(r, strings.TrimPrefix(redirect.To, u.Path)); r2 != nil {
- requestURI = redirect.To
- r = r2
+ if !redirect.Force {
+ path := filepath.Clean(strings.TrimPrefix(requestURI, u.Path))
+ fi, err := f.c.hugo().BaseFs.PublishFs.Stat(path)
+ if err == nil {
+ if fi.IsDir() {
+ // There will be overlapping directories, so we
+ // need to check for a file.
+ _, err = f.c.hugo().BaseFs.PublishFs.Stat(filepath.Join(path, "index.html"))
+ doRedirect = err != nil
+ } else {
+ doRedirect = false
+ }
+
+ }
+ }
+
+ if doRedirect {
+ if redirect.Status == 200 {
+ if r2 := f.rewriteRequest(r, strings.TrimPrefix(redirect.To, u.Path)); r2 != nil {
+ requestURI = redirect.To
+ r = r2
+ }
+ } else {
+ w.Header().Set("Content-Type", "")
+ http.Redirect(w, r, redirect.To, redirect.Status)
+ return
}
- } else {
- w.Header().Set("Content-Type", "")
- http.Redirect(w, r, redirect.To, redirect.Status)
- return
}
}
@@ -416,7 +435,6 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
fileserver := decorate(http.FileServer(fs))
mu := http.NewServeMux()
-
if u.Path == "" || u.Path == "/" {
mu.Handle("/", fileserver)
} else {