Giter Club home page Giter Club logo

caddy-netlify-redirects's Introduction

caddy-netlify-redirects

WHY?

  • Because caddy doesn't have a simple regex redirect sytnax module
  • Because caddy doesn't have a simple "variable replacement" redirect module

The netlify redirect syntax is simple and easy to use, so why not use it?

*** For use with Caddy2.7.6 and Golang 1.21 ***

INCLUDES FIXES FOR REDIRECT LOOPS.

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.7.6-builder AS builder

RUN xcaddy build \
   --with github.com/christophcemper/caddy-netlify-redirects/v2
   
FROM caddy:2.7.6-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

Or instead of the docker stuff, just clone the caddy repo and run xcaddy build with the module added.

git clone https://github.com/caddyserver/caddy.git
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
xcaddy build --with github.com/christophcemper/caddy-netlify-redirects 

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
}

caddy-netlify-redirects's People

Contributors

samvaughton avatar christophcemper avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.