Files
fiber/docs/middleware/rewrite.md
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.4 KiB

id
id
rewrite

Rewrite

The Rewrite middleware remaps the request path using custom rules, helping with backward compatibility and cleaner URLs.

Signatures

func New(config ...Config) fiber.Handler

Config

Property Type Description Default
Next func(fiber.Ctx) bool Skip when function returns true. nil
Rules map[string]string Map paths to new values; use $1, $2 for wildcard captures. (Required)

:::note Rules are stored in a map, so iteration order is undefined. Avoid overlapping patterns if precedence matters. :::

Examples

package main

import (
    "github.com/gofiber/fiber/v3"
    "github.com/gofiber/fiber/v3/middleware/rewrite"
)

func main() {
    app := fiber.New()

    app.Use(rewrite.New(rewrite.Config{
      Rules: map[string]string{
        "/old":   "/new",
        "/old/*": "/new/$1",
      },
    }))

    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