Redirect

Redirects middleware provides an HTTP redirect to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code.

Installation

  1. go get -u github.com/gofiber/redirect

Signature

  1. redirect.New(config ...Config) func(*Ctx)

Example

  1. package main
  2.  
  3. import (
  4. "github.com/gofiber/fiber"
  5. "github.com/gofiber/redirect"
  6. )
  7.  
  8. func main() {
  9. app := fiber.New()
  10.  
  11. app.Use(redirect.New(redirect.Config{
  12. Rules: map[string]string{
  13. "/old": "/new",
  14. "/old/*": "/new/$1",
  15. },
  16. StatusCode: 301,
  17. }))
  18.  
  19. app.Get("/new", func(c *fiber.Ctx) {
  20. c.Send("Hello, World!")
  21. })
  22. app.Get("/new/*", func(c *fiber.Ctx) {
  23. c.Send("Wildcard: ", c.Params("*"))
  24. })
  25.  
  26. app.Listen(3000)
  27. }