From 016344bae734fbdd4b0eab4275cd4dfd9c2704f6 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Tue, 24 Nov 2015 12:20:39 +0100 Subject: [PATCH] Rewrite: Use middleware.Replacer for simple rule. --- middleware/rewrite/rewrite.go | 6 +++++- middleware/rewrite/rewrite_test.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/middleware/rewrite/rewrite.go b/middleware/rewrite/rewrite.go index 885bfad2..88944f73 100644 --- a/middleware/rewrite/rewrite.go +++ b/middleware/rewrite/rewrite.go @@ -52,7 +52,11 @@ func (s SimpleRule) Rewrite(r *http.Request) bool { // take note of this rewrite for internal use by fastcgi // all we need is the URI, not full URL r.Header.Set(headerFieldName, r.URL.RequestURI()) - r.URL.Path = s.To + + // replace variables + to := path.Clean(middleware.NewReplacer(r, nil, "").Replace(s.To)) + + r.URL.Path = to return true } return false diff --git a/middleware/rewrite/rewrite_test.go b/middleware/rewrite/rewrite_test.go index 23280aee..fb047026 100644 --- a/middleware/rewrite/rewrite_test.go +++ b/middleware/rewrite/rewrite_test.go @@ -17,6 +17,7 @@ func TestRewrite(t *testing.T) { Rules: []Rule{ NewSimpleRule("/from", "/to"), NewSimpleRule("/a", "/b"), + NewSimpleRule("/b", "/b{uri}"), }, } @@ -50,6 +51,7 @@ func TestRewrite(t *testing.T) { }{ {"/from", "/to"}, {"/a", "/b"}, + {"/b", "/b/b"}, {"/aa", "/aa"}, {"/", "/"}, {"/a?foo=bar", "/b?foo=bar"},