Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect slashes drops the Query params #334

Closed
kynrai opened this issue Jul 24, 2018 · 2 comments
Closed

Redirect slashes drops the Query params #334

kynrai opened this issue Jul 24, 2018 · 2 comments

Comments

@kynrai
Copy link

kynrai commented Jul 24, 2018

I have come across an issue where redirect slash middleware will drop the url query params.

I have managed to temporarily fix it with:

// RedirectSlashes is a middleware that will match request paths with a trailing
// slash and redirect to the same path, less the trailing slash.
func RedirectSlashes(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var path string
		rctx := chi.RouteContext(r.Context())
		if rctx.RoutePath != "" {
			path = rctx.RoutePath
		} else {
			path = r.URL.Path
		}
		if len(path) > 1 && path[len(path)-1] == '/' {
			path = fmt.Sprintf("%s?%s", path[:len(path)-1], r.URL.RawQuery)
			http.Redirect(w, r, path, 301)
			return
		}
		next.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}

But it feels somewhat incomplete

@VojtechVitek
Copy link
Contributor

Thanks! Can you please provide reproducer example and a diff or a Pull Request?

@pkieltyka
Copy link
Member

fixed in v4 PR #378

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants