mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
* Add support for consistent documentation using markdownlint * Only run workflow during changes to markdown files * Fix more inconsistencies * Fixes to markdown under .github/ * More fixes * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Fix typo in limiter docs * Add missing space before code-block * Add check for dead-links * Add write-good * Remove legacy README files * Fix glob for skipping .md files * Use paths-ignore instead --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
60 lines
1.5 KiB
Markdown
60 lines
1.5 KiB
Markdown
---
|
|
id: rewrite
|
|
---
|
|
|
|
# Rewrite
|
|
|
|
Rewrite middleware rewrites the URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links.
|
|
|
|
## Signatures
|
|
|
|
```go
|
|
func New(config ...Config) fiber.Handler
|
|
```
|
|
|
|
## Config
|
|
|
|
| Property | Type | Description | Default |
|
|
|:---------|:------------------------|:-----------------------------------------------------------------------------------------------------|:-----------|
|
|
| Next | `func(fiber.Ctx) bool` | Next defines a function to skip middleware. | `nil` |
|
|
| Rules | `map[string]string` | Rules defines the URL path rewrite rules. The values captured in asterisk can be retrieved by index. | (Required) |
|
|
|
|
### Examples
|
|
|
|
```go
|
|
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
|
|
|
|
```bash
|
|
curl http://localhost:3000/old
|
|
curl http://localhost:3000/old/hello
|
|
```
|