naddy

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2021 License: MIT Imports: 14 Imported by: 0

README

caddy-netlify-redirects

For use with Caddy2.

Warning: This module does not handle query string matching the same as Netlify. It will only perform exact match.

Enables Caddy to use Netlify's _redirect file format

This module tries to replicate the way Netlify's _redirects file works.

It does support:

  • Host redirection
  • Path redirection
  • Other status codes such as 410 Gone (with a redirect after returning the 410)

It does not (currently) support:

  • Header matching
  • Query string matching
  • HTTP -> HTTPS redirection

If you wish to add these features, please open an issue/PR.

Development

See https://caddyserver.com/docs/extending-caddy and https://github.com/caddyserver/xcaddy

xcaddy run xcaddy run --config caddy.json

Building via Docker

As an example, within a dockerfile you can build Caddy with this custom module:

FROM caddy:2.4.3-builder AS builder

RUN xcaddy build \
   --with github.com/samvaughton/caddy-netlify-redirects/v2
   
FROM caddy:2.4.3-alpine as serve

COPY --from=builder /usr/bin/caddy /usr/bin/caddy
COPY ./Caddyfile /etc/caddy/Caddyfile

# Copy over your built assets for your webapp, this could be from gatbsy which includes a _redirects file
COPY --from=node-builder /usr/src/app/packages/rentivo-gatsby-site/public /srv

Config

You will need to set the order of the module with this line:

order netlify_redirects before redir
Caveat

If the _redirects file does not exist when using the import directive, Caddy will fail to start. You can fix this by using a glob pattern: import _redirects*

Adding redirects

Put a netlify_redirects directive within the Caddyfile eg:

netlify_redirects {
   /:param/here/:test/two /:param/:test/:two 302
   /hello/* /redirected/:splat
   /:param/hello/* /redirected/:param/:splat
}

You can also import a _netlify file:

netlify_redirects {
   import /srv/_redirects
}

Documentation

Index

Constants

View Source
const ModuleName = "netlify_redirects"

Variables

This section is empty.

Functions

func ParseUrlWithContext

func ParseUrlWithContext(urlStr string, ctx *MatchContext) (*url.URL, error)

Types

type MatchContext

type MatchContext struct {
	Scheme      string
	OriginalUrl *url.URL
}

type MatchResult

type MatchResult struct {
	Match      *urlpath.Match
	ResolvedTo *url.URL
	Source     redirects.Rule

	IsMatched      bool
	IsHostRedirect bool

	Error error
}

func MatchUrlToRule

func MatchUrlToRule(rule redirects.Rule, reqUrl *url.URL, ctx *MatchContext) MatchResult

type Middleware

type Middleware struct {
	Logger    *zap.Logger
	Redirects []redirects.Rule
}

func (Middleware) CaddyModule

func (Middleware) CaddyModule() caddy.ModuleInfo

func (*Middleware) Provision

func (m *Middleware) Provision(ctx caddy.Context) error

func (Middleware) ServeHTTP

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

Jump to

Keyboard shortcuts

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