Wildcard Routes
Capture and forward dynamic URL branches using wildcard routes for static files, frontend applications, mounted services, and catch-all handlers.
Wildcard routes capture the remaining unmatched part of the URL path.
They are useful for:
- static file serving
- frontend SPA routing
- mounted applications
- reverse proxies
- catch-all handlers
- nested services
- asset delivery
Wildcard matching allows a route to handle an entire URL branch instead of a single fixed path.
Basic Wildcard Syntax
Wildcard routes use the * syntax.
Example:
r.GET("/files/*", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
w.Write([]byte(req.URL.Path))
})Example requests:
/files/report.pdf
/files/images/logo.png
/files/archive/2026/05/data.jsonAll requests beginning with:
/files/are matched by the same handler.
Example
package main
import (
"net/http"
"github.com/netlifeguru/router"
)
func main() {
r := router.New()
r.GET("/files/*", func(w http.ResponseWriter, req *http.Request, ctx *router.Context) {
w.Write([]byte(req.URL.Path))
})
r.ListenAndServe(":8000")
}Request:
/files/images/logo.pngResponse:
/files/images/logo.pngWildcards with Mount
Wildcards are commonly used together with mounted handlers.
Example:
r.Mount("/assets/*", http.FileServer(http.Dir("./public")))This forwards all requests under:
/assets/to the mounted file server.
Examples:
/assets/style.css
/assets/js/app.js
/assets/images/logo.pngThis pattern is commonly used for:
- static assets
- frontend applications
- Swagger UI
- GraphQL playgrounds
- mounted services
- reverse proxies
Wildcards with MountFunc
Wildcard routes also work with MountFunc.
r.MountFunc("/services/*", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(req.URL.Path))
})This allows standard net/http handlers to process entire URL branches.
SPA Frontend Routing
Wildcard routes are useful for Single Page Applications.
Example:
r.Mount("/app/*", http.FileServer(http.Dir("./frontend")))Requests such as:
/app/dashboard
/app/settings/profile
/app/users/42can all be handled by the mounted frontend application.
Wildcard Matching Behavior
Wildcards always match the remaining path after the route prefix.
Example route:
/files/*Matches:
/files/a.txt
/files/images/logo.png
/files/archive/2026/report.pdfDoes not match:
/file/test.txtbecause the prefix differs.
Notes
Wildcard routes are designed for branch-style routing where an entire subtree of URLs should be handled by the same handler.
For single dynamic segments, prefer parameterized routes:
/users/{id}instead of:
/users/*This keeps route matching more explicit and easier to maintain.