From a34b6fd0452a586b92927e9a51573f3a16719b56 Mon Sep 17 00:00:00 2001 From: Kai H Date: Wed, 6 May 2015 20:38:36 +0200 Subject: [PATCH] Fixed: Trailing slash redirection when using pages. --- server/pages.go | 12 ++++++++++-- templates/handlebars.go | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/server/pages.go b/server/pages.go index c077e8ce..dc4f890a 100644 --- a/server/pages.go +++ b/server/pages.go @@ -3,16 +3,24 @@ package server import ( "github.com/dimfeld/httptreemux" "github.com/kabukky/journey/filenames" + "github.com/kabukky/journey/helpers" "net/http" "path/filepath" + "strings" ) func pagesHandler(w http.ResponseWriter, r *http.Request, params map[string]string) { - http.ServeFile(w, r, filepath.Join(filenames.PagesFilepath, params["filepath"])) + path := filepath.Join(filenames.PagesFilepath, params["filepath"]) + // If the path points to a directory, add a trailing slash to the path (needed if the page loads relative assets). + if helpers.IsDirectory(path) && !strings.HasSuffix(r.RequestURI, "/") { + http.Redirect(w, r, r.RequestURI+"/", 301) + return + } + http.ServeFile(w, r, path) return } func InitializePages(router *httptreemux.TreeMux) { // For serving standalone projects or pages saved in in content/pages - router.GET("/pages/*filepath/", pagesHandler) + router.GET("/pages/*filepath", pagesHandler) } diff --git a/templates/handlebars.go b/templates/handlebars.go index 8884b5b8..90fbdc82 100644 --- a/templates/handlebars.go +++ b/templates/handlebars.go @@ -741,7 +741,7 @@ func atOddFunc(helper *structure.Helper, values *structure.RequestData) []byte { } func nameFunc(helper *structure.Helper, values *structure.RequestData) []byte { - // If tag (commented out the code for generating a link. Ghost doesn't seem to do that either. + // If tag (commented out the code for generating a link. Ghost doesn't seem to do that either). if values.CurrentHelperContext == 2 { // tag //var buffer bytes.Buffer //buffer.WriteString("