From d9ebc5398ac6b3583eb848590ac94046f01ea4cb Mon Sep 17 00:00:00 2001 From: Maxime Date: Sun, 12 Jul 2015 21:22:15 +0200 Subject: [PATCH] Changes regarding review Use path.Join and then check if the request had a slash at the end to place it again. --- middleware/redirect/redirect.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/middleware/redirect/redirect.go b/middleware/redirect/redirect.go index 00ca9483..2c5de410 100644 --- a/middleware/redirect/redirect.go +++ b/middleware/redirect/redirect.go @@ -7,7 +7,8 @@ import ( "html" "net/http" "net/url" - "regexp" + "path" + "strings" "github.com/mholt/caddy/middleware" ) @@ -27,9 +28,10 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error if err != nil { return http.StatusInternalServerError, err } - newPath := toURL.Host + toURL.Path + r.URL.Path - rmSlashs := regexp.MustCompile("//+") - newPath = rmSlashs.ReplaceAllString(newPath, "/") + newPath := path.Join(toURL.Host, toURL.Path, r.URL.Path) + if strings.HasSuffix(r.URL.Path, "/") { + newPath = newPath + "/" + } newPath = toURL.Scheme + "://" + newPath parameters := toURL.Query() for k, v := range r.URL.Query() {