github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/redirect.md (about)

     1  ---
     2  id: redirect
     3  title: Redirect
     4  ---
     5  
     6  Redirection middleware for Fiber.
     7  
     8  ## Signatures
     9  
    10  ```go
    11  func New(config ...Config) fiber.Handler
    12  ```
    13  
    14  ## Examples
    15  
    16  ```go
    17  package main
    18  
    19  import (
    20    "github.com/gofiber/fiber/v2"
    21    "github.com/gofiber/fiber/v2/middleware/redirect"
    22  )
    23  
    24  func main() {
    25    app := fiber.New()
    26    
    27    app.Use(redirect.New(redirect.Config{
    28      Rules: map[string]string{
    29        "/old":   "/new",
    30        "/old/*": "/new/$1",
    31      },
    32      StatusCode: 301,
    33    }))
    34    
    35    app.Get("/new", func(c *fiber.Ctx) error {
    36      return c.SendString("Hello, World!")
    37    })
    38    app.Get("/new/*", func(c *fiber.Ctx) error {
    39      return c.SendString("Wildcard: " + c.Params("*"))
    40    })
    41    
    42    app.Listen(":3000")
    43  }
    44  ```
    45  
    46  **Test:**
    47  
    48  ```curl
    49  curl http://localhost:3000/old
    50  curl http://localhost:3000/old/hello
    51  ```
    52  
    53  ## Config
    54  
    55  ```go
    56  // Config defines the config for middleware.
    57  type Config struct {
    58  	// Filter defines a function to skip middleware.
    59  	// Optional. Default: nil
    60  	Next func(*fiber.Ctx) bool
    61  
    62  	// Rules defines the URL path rewrite rules. The values captured in asterisk can be
    63  	// retrieved by index e.g. $1, $2 and so on.
    64  	// Required. Example:
    65  	// "/old":              "/new",
    66  	// "/api/*":            "/$1",
    67  	// "/js/*":             "/public/javascripts/$1",
    68  	// "/users/*/orders/*": "/user/$1/order/$2",
    69  	Rules map[string]string
    70  
    71  	// The status code when redirecting
    72  	// This is ignored if Redirect is disabled
    73  	// Optional. Default: 302 (fiber.StatusFound)
    74  	StatusCode int
    75  
    76  	rulesRegex map[*regexp.Regexp]string
    77  }
    78  ```
    79  
    80  ## Default Config
    81  
    82  ```go
    83  var ConfigDefault = Config{
    84  	StatusCode: fiber.StatusFound,
    85  }
    86  ```