route all *.php requests #2741
|
Hello there! Have an idea how to route queries like https://my.host/blabla/bla/some.php ? |
Answered by
adilburaksen
May 11, 2026
Replies: 1 comment
|
Echo's router doesn't support glob patterns like The cleanest approach is to use a catch-all route and check the file extension in the handler: e.GET("/*", func(c echo.Context) error {
path := c.Request().URL.Path
if strings.HasSuffix(path, ".php") {
return phpHandler(c)
}
return echo.ErrNotFound
})Or with middleware if you want it applied before routing reaches other handlers: e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if strings.HasSuffix(c.Request().URL.Path, ".php") {
return phpHandler(c)
}
return next(c)
}
})If you need to match the path segments before e.GET("/*", func(c echo.Context) error {
wildcard := c.Param("*") // "blabla/bla/some.php"
if !strings.HasSuffix(wildcard, ".php") {
return echo.ErrNotFound
}
name := strings.TrimSuffix(wildcard, ".php")
_ = name // use as needed
return phpHandler(c)
}) |
0 replies
Answer selected by
efkz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Echo's router doesn't support glob patterns like
*.phpin path definitions — the*wildcard only works as a catch-all at the end of a path (/*).The cleanest approach is to use a catch-all route and check the file extension in the handler:
Or with middleware if you want it applied before routing reaches other handlers: