跳到主要内容
版本: v2.x

重定向

Fiber 的重定向中间件。

签名

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

示例

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")
}

测试

curl http://localhost:3000/old
curl http://localhost:3000/old/hello

配置

属性类型描述默认值
下一页func(*fiber.Ctx) boolFilter 定义了一个函数来跳过中间件。nil
规则map[string]stringRules 定义了 URL 路径重写规则。星号中捕获的值可以通过索引(例如 $1, $2 等)来获取。必填
状态码int重定向时使用的状态码。如果重定向被禁用,此设置将被忽略。302 临时重定向

默认配置

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