Redirect
Redirect middleware maps old URLs to new ones using simple rules.
Signatures
func New(config ...Config) fiber.Handler
Examples
package main
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/redirect"
)
func main() {
app := fiber.New()
app.Use(redirect.New(redirect.Config{
Rules: map[string]string{
"/old": "/new",
"/old/*": "/new/$1",
},
StatusCode: fiber.StatusMovedPermanently,
}))
app.Get("/new", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Get("/new/*", func(c fiber.Ctx) error {
return c.SendString("Wildcard: " + c.Params("*"))
})
app.Listen(":3000")
}
Test
curl http://localhost:3000/old
curl http://localhost:3000/old/hello
Config
Property | Type | Description | Default |
---|---|---|---|
Next | func(fiber.Ctx) bool | Skip when function returns true. | nil |
Rules | map[string]string | Map paths to new ones; $1 , $2 insert params. | Required |
StatusCode | int | HTTP code for redirects. | 302 Temporary Redirect |
Default Config
var ConfigDefault = Config{
StatusCode: fiber.StatusFound,
}