Netlify, Cloudflare and IPFS allow use of a _redirects
file to direct their HTTP servers to send specific HTTP status codes and Location
headers for paths that don’t exist in the static files they serve.
I’d like to see Hugo’s local dev server respect these files, so as to better test how my sites will work when published. I’m willing to build/submit a PR for this feature, if it seems desirable, and the Hugo team deem it suitable of maintenance costs. Is this interesting to you?
A sample file:
# Hash-starting lines are comments, empty lines are ignored
/blog/:name /posts/:name 307
/posts/example-product-launch/ https://example.com 308
* /404.html 404
Instructions are processed top to bottom; this file will:
- Serve existing files without checking this file
- Compare the inbound URL against
/blog/<anything>
, if it matches it will send anHTTP 307
response code withLocation: /posts/<anything>
(replacing<anything>
with whatever matched) - Only if the previous rule(s) didn’t match, it’ll check the inbound URL against
/posts/example-product-launch/
, and send anHTTP 308
redirect withLocation: https://example.com
if it matches. - Only if the previous rule(s) didn’t match, it’ll return the contents of
404.html
with anHTTP 404
response code.
This appears simple, but there are plenty of edge-cases. There is MIT licensed Go code for processing this file in IPFS’ go-ipfs-redirects-file repo that I’d use/reuse.
Some open thoughts & questions:
- I’d expect this to be an opt-in feature, so Hugo users have opportunity to understand that the functionality comes from their hosting provider (Cloudflare, Netlify, IPFS) and not with other hosts.
- Though these providers allow a
_redirects
file to be in any directory, I believe it’d be reasonable for Hugo to constrain this file to only living at the root (ie. stored atstatic/_redirects
before build)