traefik_dynamic_redirects

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Traefik Dynamic Redirects

release report OpenSSF Scorecard Coverage Status license

A Traefik middleware plugin for dynamic redirects based on cookies and the Accept-Language header.

This plugin allows defining multiple dynamic redirect rules in a single Traefik Middleware configuration. It is useful for root-page routing such as:

  • redirecting authenticated users to an application/home page;
  • redirecting anonymous users based on a locale cookie;
  • falling back to the browser Accept-Language header;
  • falling back to a default target URL.

Redirect behavior

For each matching sourceURL, the plugin resolves the redirect target in this order:

  1. authenticatedCookie + authenticatedTarget
  2. localeCookie + localeTargets
  3. Accept-Language + localeTargets
  4. defaultTarget

If none of the configured redirects match the request URL, the request is passed to the next handler.

Redirect fields

Key Description
sourceURL Absolute source URL to match. Query strings and fragments are not supported in sourceURL.
statusCode Redirect status code: 301, 302, 303, 307, 308. Defaults to 302 when omitted.
preserveQueryString Boolean. When true, appends the original request query string to the selected target URL.
authenticatedCookie Cookie name used to detect authenticated users.
authenticatedTarget Absolute target URL used when authenticatedCookie exists and has a non-empty value.
localeCookie Cookie name used to select a localized redirect target.
defaultTarget Absolute fallback target URL.
localeTargets Map of locale code to absolute target URL, for example en, de, fr, it, es.

Configuration

Example Middleware:

apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: dynamic-redirects
spec:
  plugin:
    dynamicRedirects:
      redirects:
        - sourceURL: https://example.com/
          statusCode: 302
          preserveQueryString: true
          authenticatedCookie: Authentication
          authenticatedTarget: https://example.com/home/
          localeCookie: locale
          defaultTarget: https://example.com/en/
          localeTargets:
            de: https://example.com/de/
            es: https://example.com/es/
            fr: https://example.com/fr/
            it: https://example.com/it/

        - sourceURL: https://www.example.com/
          statusCode: 302
          preserveQueryString: true
          authenticatedCookie: Authentication
          authenticatedTarget: https://example.com/home/
          localeCookie: locale
          defaultTarget: https://example.com/en/
          localeTargets:
            de: https://example.com/de/
            es: https://example.com/es/
            fr: https://example.com/fr/
            it: https://example.com/it/

Static configuration

Enable the plugin in Traefik static configuration:

experimental:
  plugins:
    dynamicRedirects:
      moduleName: github.com/DoodleScheduling/traefik-dynamic-redirects
      version: v0.0.1

Example results

Authenticated user:

GET https://example.com/
Cookie: Authentication=abc

Redirects to:

https://example.com/home/

Locale cookie:

GET https://example.com/
Cookie: locale=es

Redirects to:

https://example.com/es/

Accept-Language fallback:

GET https://example.com/
Accept-Language: fr-FR,fr;q=0.9,en;q=0.8

Redirects to:

https://example.com/fr/

Default fallback:

GET https://example.com/

Redirects to:

https://example.com/en/

Plugin catalog metadata

The .traefik.yml metadata should match the plugin module name:

displayName: Dynamic Redirects
iconPath: .assets/icon.png
import: github.com/DoodleScheduling/traefik-dynamic-redirects
summary: Middleware for dynamic redirects based on cookies and Accept-Language.
type: middleware
testData:
  redirects:
    - sourceURL: https://example.com/
      statusCode: 302
      preserveQueryString: true
      authenticatedCookie: Authentication
      authenticatedTarget: https://example.com/home/
      localeCookie: locale
      defaultTarget: https://example.com/en/
      localeTargets:
        de: https://example.com/de/
        es: https://example.com/es/
        fr: https://example.com/fr/
        it: https://example.com/it/
    - sourceURL: https://www.example.com/
      statusCode: 302
      preserveQueryString: true
      authenticatedCookie: Authentication
      authenticatedTarget: https://example.com/home/
      localeCookie: locale
      defaultTarget: https://example.com/en/
      localeTargets:
        de: https://example.com/de/
        es: https://example.com/es/
        fr: https://example.com/fr/
        it: https://example.com/it/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

Types

type Config

type Config struct {
	Redirects []Redirect `json:"redirects,omitempty"`
}

func CreateConfig

func CreateConfig() *Config

type DynamicRedirects

type DynamicRedirects struct {
	// contains filtered or unexported fields
}

func (*DynamicRedirects) ServeHTTP

func (dynamicRedirects *DynamicRedirects) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Redirect

type Redirect struct {
	SourceURL           string            `json:"sourceURL,omitempty"`
	StatusCode          int               `json:"statusCode,omitempty"`
	PreserveQueryString bool              `json:"preserveQueryString,omitempty"`
	AuthenticatedCookie string            `json:"authenticatedCookie,omitempty"`
	AuthenticatedTarget string            `json:"authenticatedTarget,omitempty"`
	LocaleCookie        string            `json:"localeCookie,omitempty"`
	DefaultTarget       string            `json:"defaultTarget,omitempty"`
	LocaleTargets       map[string]string `json:"localeTargets,omitempty"`
}

type RuntimeRedirect

type RuntimeRedirect struct {
	StatusCode          int
	PreserveQueryString bool
	AuthenticatedCookie string
	AuthenticatedTarget string
	LocaleCookie        string
	DefaultTarget       string
	LocaleTargets       map[string]string
}

type Target

type Target struct {
	URL                 string
	StatusCode          int
	PreserveQueryString bool
}

Jump to

Keyboard shortcuts

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