diff --git a/pkg/middleware/org_redirect.go b/pkg/middleware/org_redirect.go index c4d5623d69e..b794f9acfdf 100644 --- a/pkg/middleware/org_redirect.go +++ b/pkg/middleware/org_redirect.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "strconv" - "strings" "github.com/grafana/grafana/pkg/services/contexthandler" "github.com/grafana/grafana/pkg/services/user" @@ -50,7 +49,7 @@ func OrgRedirect(cfg *setting.Cfg, userSvc user.Service) web.Handler { qs = fmt.Sprintf("%s&kiosk", urlParams.Encode()) } - newURL := fmt.Sprintf("%s%s?%s", cfg.AppSubURL, strings.TrimPrefix(c.Req.URL.Path, "/"), qs) + newURL := fmt.Sprintf("%s%s?%s", cfg.AppSubURL, c.Req.URL.Path, qs) c.Redirect(newURL, 302) } diff --git a/pkg/middleware/org_redirect_test.go b/pkg/middleware/org_redirect_test.go index e2bc6d81075..4087eeb91c6 100644 --- a/pkg/middleware/org_redirect_test.go +++ b/pkg/middleware/org_redirect_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "github.com/grafana/grafana/pkg/services/authn" + "github.com/grafana/grafana/pkg/setting" ) func TestOrgRedirectMiddleware(t *testing.T) { @@ -62,4 +63,17 @@ func TestOrgRedirectMiddleware(t *testing.T) { require.Equal(t, 404, sc.resp.Code) }) + + middlewareScenario(t, "works correctly when grafana is served under a subpath", func(t *testing.T, sc *scenarioContext) { + sc.withIdentity(&authn.Identity{}) + + sc.m.Get("/", sc.defaultHandler) + sc.fakeReq("GET", "/?orgId=3").exec() + + require.Equal(t, 302, sc.resp.Code) + require.Equal(t, "/grafana/?orgId=3", sc.resp.Header().Get("Location")) + }, func(cfg *setting.Cfg) { + cfg.AppURL = "http://localhost:3000/grafana/" + cfg.AppSubURL = "/grafana" + }) }