重定向
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) bool | Filter 定义了一个函数来跳过中间件。 | nil |
规则 | map[string]string | Rules 定义了 URL 路径重写规则。星号中捕获的值可以通过索引(例如 $1, $2 等)来获取。 | 必填 |
状态码 | int | 重定向时使用的状态码。如果重定向被禁用,此设置将被忽略。 | 302 临时重定向 |
默认配置
var ConfigDefault = Config{
StatusCode: fiber.StatusFound,
}