Skip to main content
Version: Next

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

PropertyTypeDescriptionDefault
Nextfunc(fiber.Ctx) boolSkip when function returns true.nil
Rulesmap[string]stringMap paths to new ones; $1, $2 insert params.Required
StatusCodeintHTTP code for redirects.302 Temporary Redirect

Default Config

var ConfigDefault = Config{
StatusCode: fiber.StatusFound,
}