Middleware

middleware/redirect

github.com/gofiber/fiber/v3/middleware/redirect

import "github.com/gofiber/fiber/v3/middleware/redirect"

Variables

ConfigDefault is the default config

Source: middleware/redirect/config.go:56

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

Functions

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

Source: middleware/redirect/redirect.go:176

New creates a new middleware handler

Types

type Config

Source: middleware/redirect/config.go:22

Config defines the config for middleware.

type Config struct {
	// Next defines a function to skip middleware.
	// Optional. Default: nil
	Next func(fiber.Ctx) bool

	// Rules defines the URL path redirect rules. The values captured in asterisk can be
	// retrieved by index e.g. $1, $2 and so on.
	//
	// Deprecated: Use RuleList instead. A map has no order, so the rule answering
	// a path two rules both match is decided by a documented heuristic rather
	// than by the author. Retained for backward compatibility with existing
	// configurations.
	Rules map[string]string

	// RuleList defines the URL path redirect rules. They are tried in the order
	// given and the first rule whose From matches the request path wins, as
	// routes do; nothing reorders the list. Put the specific rules before the
	// catch-alls. Set this or the deprecated Rules, not both.
	//
	// Example:
	// {From: "/old", To: "/new"},
	// {From: "/api/*", To: "/$1"},
	// {From: "/users/*/orders/*", To: "/user/$1/order/$2"},
	RuleList []Rule

	// The status code when redirecting
	// This is ignored if Redirect is disabled
	// Optional. Default: 302 Temporary Redirect
	StatusCode int
	// contains filtered or unexported fields
}
Fields
  • Next func(fiber.Ctx) bool

    Next defines a function to skip middleware. Optional. Default: nil

  • Rules map[string]string

    Rules defines the URL path redirect rules. The values captured in asterisk can be retrieved by index e.g. $1, $2 and so on.

    Deprecated: Use RuleList instead. A map has no order, so the rule answering a path two rules both match is decided by a documented heuristic rather than by the author. Retained for backward compatibility with existing configurations.

  • RuleList []Rule

    RuleList defines the URL path redirect rules. They are tried in the order given and the first rule whose From matches the request path wins, as routes do; nothing reorders the list. Put the specific rules before the catch-alls. Set this or the deprecated Rules, not both.

    Example: {From: "/old", To: "/new"}, {From: "/api/*", To: "/$1"}, {From: "/users/*/orders/*", To: "/user/$1/order/$2"},

  • StatusCode int

    The status code when redirecting This is ignored if Redirect is disabled Optional. Default: 302 Temporary Redirect

type Rule

Source: middleware/redirect/config.go:10

Rule is one redirect: the path pattern to match and the target to send the client to. The values captured in asterisk can be retrieved by index e.g. $1, $2 and so on.

type Rule struct {
	// From is the path pattern, where "*" matches a run of any length. The key
	// reaches the compiled pattern as a regular expression, so the other regexp
	// metacharacters keep their meaning: "/a.b" also matches "/aXb".
	From string

	// To is the redirect target, where "$1", "$2" and so on stand for what the
	// asterisks of From captured.
	To string
}
Fields
  • From string

    From is the path pattern, where "*" matches a run of any length. The key reaches the compiled pattern as a regular expression, so the other regexp metacharacters keep their meaning: "/a.b" also matches "/aXb".

  • To string

    To is the redirect target, where "$1", "$2" and so on stand for what the asterisks of From captured.