-
I would like to rewrite the URL for a specific route. mux.Route("/endpoint", func(r chi.Router) {
r.Use(middleware.RewriteRoute)
r.Route("/{source}", func(r chi.Router) {
r.Get("/handler", myHandler)
})
}) Though, when moving the middleware to the outer scope it is working. mux.Use(middleware.RewriteRoute)
mux.Route("/endpoint", func(r chi.Router) {
r.Route("/{source}", func(r chi.Router) {
r.Get("/handler", myHandler)
})
}) Can someone please explain what's wrong with my idea? |
Beta Was this translation helpful? Give feedback.
Answered by
ousloob
Feb 10, 2025
Replies: 1 comment
-
@phhoef |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
phhoef
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@phhoef
When you use
mux.Route("/endpoint", func(r chi.Router) {...})
, Chi creates a subrouter that is isolated from the parent router mux. When you attachmiddleware.RewriteRoute
inside this subrouter, it only affects the routes defined inside that block and does not alter the URL before it reaches the subrouter.