Files
Juan Calderon-PerezGitHubcoderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
e4737ee937 📒 docs: Full audit of documentation (#3717)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2025-08-29 12:34:19 +02:00

1.5 KiB

id
id
redirect

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,
}