mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-08 08:39:41 +00:00
* ♻️ refactor: merge adaptor, helmet, keyauth, redirect, rewrite middlewares to core
* fix linting issues
* fix linting issues
* fix linting issues
* update
1.6 KiB
1.6 KiB
id, title
id | title |
---|---|
redirect | Redirect |
Redirection middleware for Fiber.
Signatures
func New(config ...Config) fiber.Handler
Examples
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/redirect"
)
func main() {
app := fiber.New()
app.Use(redirect.New(redirect.Config{
Rules: map[string]string{
"/old": "/new",
"/old/*": "/new/$1",
},
StatusCode: 301,
}))
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
// Config defines the config for middleware.
type Config struct {
// Filter defines a function to skip middleware.
// Optional. Default: nil
Next func(*fiber.Ctx) bool
// Rules defines the URL path rewrite rules. The values captured in asterisk can be
// retrieved by index e.g. $1, $2 and so on.
// Required. Example:
// "/old": "/new",
// "/api/*": "/$1",
// "/js/*": "/public/javascripts/$1",
// "/users/*/orders/*": "/user/$1/order/$2",
Rules map[string]string
// The status code when redirecting
// This is ignored if Redirect is disabled
// Optional. Default: 302 (fiber.StatusFound)
StatusCode int
rulesRegex map[*regexp.Regexp]string
}
Default Config
var ConfigDefault = Config{
StatusCode: fiber.StatusFound,
}