redirect

package
v3.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

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

ConfigDefault is the default config

Functions

func New

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

New creates a new middleware handler

Types

type Config

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
}

Config defines the config for middleware.

type Rule added in v3.6.0

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
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL