redirects

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Redirect

Redirect format parser to match Netlify's format.

Example

# Implicit 301 redirects
/home              /
/blog/my-post.php  /blog/my-post
/news              /blog
/google            https://www.google.com

# Redirect with a 301
/home         /              301

# Redirect with a 302
/my-redirect  /              302

# Rewrite a path
/pass-through /index.html    200

# Show a custom 404 for this path
/ecommerce    /store-closed  404

# Single page app rewrite
/*    /index.html   200

# Proxying
/api/*  https://api.example.com/:splat  200

# Forcing
/app/*  /app/index.html  200!

# Params
/	/something	302	foo=bar
/	/something	302	foo=bar bar=baz

yields

[
{
  "From": "/home",
  "To": "/",
  "Status": 301,
  "Force": false,
  "Params": null
},
{
  "From": "/blog/my-post.php",
  "To": "/blog/my-post",
  "Status": 301,
  "Force": false,
  "Params": null
},
{
  "From": "/news",
  "To": "/blog",
  "Status": 301,
  "Force": false,
  "Params": null
},
{
  "From": "/google",
  "To": "https://www.google.com",
  "Status": 301,
  "Force": false,
  "Params": null
},
{
  "From": "/home",
  "To": "/",
  "Status": 301,
  "Force": false,
  "Params": null
},
{
  "From": "/my-redirect",
  "To": "/",
  "Status": 302,
  "Force": false,
  "Params": null
},
{
  "From": "/pass-through",
  "To": "/index.html",
  "Status": 200,
  "Force": false,
  "Params": null
},
{
  "From": "/ecommerce",
  "To": "/store-closed",
  "Status": 404,
  "Force": false,
  "Params": null
},
{
  "From": "/*",
  "To": "/index.html",
  "Status": 200,
  "Force": false,
  "Params": null
},
{
  "From": "/api/*",
  "To": "https://api.example.com/:splat",
  "Status": 200,
  "Force": false,
  "Params": null
},
{
  "From": "/app/*",
  "To": "/app/index.html",
  "Status": 200,
  "Force": true,
  "Params": null
},
{
  "From": "/",
  "To": "/something",
  "Status": 302,
  "Force": false,
  "Params": {
    "foo": "bar"
  }
},
{
  "From": "/",
  "To": "/something",
  "Status": 302,
  "Force": false,
  "Params": {
    "bar": "baz",
    "foo": "bar"
  }
}
]

GoDoc

Documentation

Overview

Package redirects provides Netlify style _redirects file format parsing.

Example
package main

import (
	"encoding/json"
	"os"
	"reflect"

	"codeberg.org/git-pages/go-redirects"
)

func main() {
	h := redirects.Must(redirects.ParseString(`
		# Implicit 301 redirects
		/home              /
		/blog/my-post.php  /blog/my-post
		/news              /blog
		/google            https://www.google.com

		# Redirect with a 301
		/home         /              301

		# Redirect with a 302
		/my-redirect  /              302

		# Rewrite a path
		/pass-through /index.html    200

		# Show a custom 404 for this path
		/ecommerce    /store-closed  404

		# Single page app rewrite
		/*    /index.html   200

		# Proxying
		/api/*  https://api.example.com/:splat  200

		# Forcing
		/app/*  /app/index.html  200!

		# Params
		/	/something	302	foo=bar
		/	/something	302	foo=bar bar=baz
	`))

	s := redirects.Must(redirects.UnparseString(h))
	g := redirects.Must(redirects.ParseString(s))
	if !reflect.DeepEqual(h, g) {
		panic("not equal")
	}

	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(h)
}
Output:
	[
  {
    "From": "/home",
    "To": "/",
    "Status": 301,
    "Force": false,
    "Params": null
  },
  {
    "From": "/blog/my-post.php",
    "To": "/blog/my-post",
    "Status": 301,
    "Force": false,
    "Params": null
  },
  {
    "From": "/news",
    "To": "/blog",
    "Status": 301,
    "Force": false,
    "Params": null
  },
  {
    "From": "/google",
    "To": "https://www.google.com",
    "Status": 301,
    "Force": false,
    "Params": null
  },
  {
    "From": "/home",
    "To": "/",
    "Status": 301,
    "Force": false,
    "Params": null
  },
  {
    "From": "/my-redirect",
    "To": "/",
    "Status": 302,
    "Force": false,
    "Params": null
  },
  {
    "From": "/pass-through",
    "To": "/index.html",
    "Status": 200,
    "Force": false,
    "Params": null
  },
  {
    "From": "/ecommerce",
    "To": "/store-closed",
    "Status": 404,
    "Force": false,
    "Params": null
  },
  {
    "From": "/*",
    "To": "/index.html",
    "Status": 200,
    "Force": false,
    "Params": null
  },
  {
    "From": "/api/*",
    "To": "https://api.example.com/:splat",
    "Status": 200,
    "Force": false,
    "Params": null
  },
  {
    "From": "/app/*",
    "To": "/app/index.html",
    "Status": 200,
    "Force": true,
    "Params": null
  },
  {
    "From": "/",
    "To": "/something",
    "Status": 302,
    "Force": false,
    "Params": {
      "foo": "bar"
    }
  },
  {
    "From": "/",
    "To": "/something",
    "Status": 302,
    "Force": false,
    "Params": {
      "bar": "baz",
      "foo": "bar"
    }
  }
]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must[T any](value T, err error) T

Panics if there is an error.

func Unparse

func Unparse(w io.Writer, rules []Rule) (err error)

Unparse serializes to the given writer.

func UnparseString

func UnparseString(rules []Rule) (result string, err error)

UnparseString serializes to a string.

Types

type Params

type Params map[string]interface{}

Params is a map of key/value pairs.

func (*Params) Get

func (p *Params) Get(key string) interface{}

Get returns the key value.

func (*Params) Has

func (p *Params) Has(key string) bool

Has returns true if the param is present.

type Rule

type Rule struct {
	// From is the path which is matched to perform the rule.
	From string

	// To is the destination which may be relative, or absolute
	// in order to proxy the request to another URL.
	To string

	// Status is one of the following:
	//
	// - 3xx a redirect
	// - 200 a rewrite
	// - defaults to 301 redirect
	//
	// When proxying this field is ignored.
	//
	Status int

	// Force is used to force a rewrite or redirect even
	// when a response (or static file) is present.
	Force bool

	// Params is an optional arbitrary map of key/value pairs.
	Params Params
}

A Rule represents a single redirection or rewrite rule.

func Parse

func Parse(r io.Reader) (rules []Rule, err error)

Parse the given reader.

func ParseString

func ParseString(s string) ([]Rule, error)

ParseString parses the given string.

func (*Rule) IsProxy

func (r *Rule) IsProxy() bool

IsProxy returns true if it's a proxy rule (aka contains a hostname).

func (*Rule) IsRewrite

func (r *Rule) IsRewrite() bool

IsRewrite returns true if the rule represents a rewrite (status 200).

Jump to

Keyboard shortcuts

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