diff --git a/middleware/slash.go b/middleware/slash.go index e48e70b4..79329051 100644 --- a/middleware/slash.go +++ b/middleware/slash.go @@ -29,9 +29,13 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc rq := c.Request() url := rq.URL() path := url.Path() + qs := url.QueryString() if path != "/" && path[len(path)-1] != '/' { path += "/" - uri := path + "?" + url.QueryString() + uri := path + if qs != "" { + uri += "?" + qs + } if config.RedirectCode != 0 { return c.Redirect(config.RedirectCode, uri) } @@ -59,10 +63,14 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu rq := c.Request() url := rq.URL() path := url.Path() + qs := url.QueryString() l := len(path) - 1 if path != "/" && path[l] == '/' { path = path[:l] - uri := path + "?" + url.QueryString() + uri := path + if qs != "" { + uri += "?" + qs + } if config.RedirectCode != 0 { return c.Redirect(config.RedirectCode, uri) } diff --git a/middleware/slash_test.go b/middleware/slash_test.go index c8776b0e..c009abaa 100644 --- a/middleware/slash_test.go +++ b/middleware/slash_test.go @@ -32,7 +32,6 @@ func TestAddTrailingSlash(t *testing.T) { }) h(c) assert.Equal(t, http.StatusMovedPermanently, rc.Status()) - assert.Equal(t, "/add-slash/?key=value", rq.URI()) assert.Equal(t, "/add-slash/?key=value", rc.Header().Get(echo.HeaderLocation)) } @@ -59,6 +58,5 @@ func TestRemoveTrailingSlash(t *testing.T) { }) h(c) assert.Equal(t, http.StatusMovedPermanently, rc.Status()) - assert.Equal(t, "/remove-slash?key=value", rq.URI()) assert.Equal(t, "/remove-slash?key=value", rc.Header().Get(echo.HeaderLocation)) }