From 6ca475def879c9ff5c12914d508d2bbad40dd596 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 12 Apr 2015 17:40:59 -0600 Subject: [PATCH] Redirect now does exact path matching like rewrite middleware --- middleware/redirect/redirect.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/middleware/redirect/redirect.go b/middleware/redirect/redirect.go index a4db80b0a..09073cd62 100644 --- a/middleware/redirect/redirect.go +++ b/middleware/redirect/redirect.go @@ -30,9 +30,9 @@ type Redirect struct { // ServeHTTP implements the middleware.Handler interface. func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { for _, rule := range rd.Rules { - if middleware.Path(r.URL.Path).Matches(rule.From) { + if r.URL.Path == rule.From { if rule.From == "/" { - // Catchall redirect preserves path (TODO: This should be made more consistent...) + // Catchall redirect preserves path (TODO: This behavior should be more standardized...) http.Redirect(w, r, strings.TrimSuffix(rule.To, "/")+r.URL.Path, rule.Code) return 0, nil } @@ -55,7 +55,6 @@ func parse(c middleware.Controller) ([]Rule, error) { rule.From = "/" rule.To = c.Val() rule.Code = 307 // TODO: Consider 301 instead? - redirects = append(redirects, rule) } else if len(args) == 3 { // From, To, and Code specified rule.From = args[0] @@ -65,10 +64,15 @@ func parse(c middleware.Controller) ([]Rule, error) { } else { rule.Code = code } - redirects = append(redirects, rule) } else { return redirects, c.ArgErr() } + + if rule.From == rule.To { + return redirects, c.Err("Redirect rule cannot allow From and To arguments to be the same.") + } + + redirects = append(redirects, rule) } return redirects, nil