Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document dynamic generation of Netlify _redirects file #1170

Open
nk9 opened this issue Jan 10, 2022 · 5 comments
Open

Document dynamic generation of Netlify _redirects file #1170

nk9 opened this issue Jan 10, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@nk9
Copy link
Contributor

nk9 commented Jan 10, 2022

I have URLs in my site which look like this:

/episode/15/the-boy-and-the-whale

I would like users who navigate to /episode/15/ to be redirected to the full URL with post slug. While this can be done with alias: in the front matter, it's cleaner to dynamically generate the Netlify _redirects file.

I have done this on another site with the TND Redirects Netlify module. But it looks like Doks may have its own way to do this already:

config.toml:

home = ["HTML", "RSS", "REDIRECTS", "HEADERS"]

# add output format for netlify _redirects
[outputFormats.REDIRECTS]
mediaType = "text/netlify"
baseName = "_redirects"
isPlainText = true
notAlternative = true

I don't see any information in the docs about how to use this output format, though. So I'd like to know if this is already possible, or if I should try to integrate TND Redirects Netlify instead.

@h-enk h-enk self-assigned this Jan 13, 2022
@h-enk
Copy link
Member

h-enk commented Jan 13, 2022

Doks can handle this (similar to the TND approach). Not documented that well though — yet 😉

Redirect rules can be appended to /layouts/index.redirects, which is then built to /public/_redirects (as you can see configured in your excerpt of config.toml above).

The default /layouts/index.redirects:

{{- range $p := .Site.Pages -}}
{{- range .Aliases }}
{{ . }} {{ $p.RelPermalink -}}
{{- end }}
{{- end -}}

# /docs/1.0/prologue/   /docs/1.0/prologue/introduction/
# /docs/1.0/help/       /docs/1.0/help/how-to-update/
# /docs/1.0/            /docs/1.0/prologue/introduction/
# /docs/                /docs/1.0/prologue/introduction/
#
# /docs/0.1/*           https://v0-1-0--doks-versioning-poc.netlify.app/docs/0.1/:splat  200
# /docs/0.2/*           https://v0-2-0--doks-versioning-poc.netlify.app/docs/0.2/:splat  200

See also the Netlify docs: Redirects and rewrites.

Hugo Aliases are usually handled by <meta http-equiv="refresh" ...> tags. These have been disabled in ./config/_default/config.toml with disableAliases = true, and instead are handled by Netlify's Redirects and rewrites. This is handled automatically and you should continue to add aliases as described in the Hugo documentation.

To be added to the Doks docs!

@h-enk h-enk changed the title Allow (or just document?) dynamic generation of Netlify _redirects file Document dynamic generation of Netlify _redirects file Jan 13, 2022
@nk9
Copy link
Contributor Author

nk9 commented Jan 13, 2022

Oh, excellent, that's just what I was hoping for. Thank you!

@h-enk h-enk transferred this issue from gethyas/doks Jan 14, 2022
@h-enk
Copy link
Member

h-enk commented Jan 8, 2024

Handled by Netlify's Redirects and rewrites in netlify.toml

@h-enk h-enk closed this as completed Jan 8, 2024
@nk9
Copy link
Contributor Author

nk9 commented Jan 8, 2024

Thanks, Henk. However, I still don't see anything in the docs about this. And it actually looks like /layouts/index.redirects is no longer there at HEAD on main. And outputFormat.REDIRECTS has been removed from config/_default/hugo.toml.

Using outputFormats.REDIRECTS and the index.redirects file is a dynamic solution, feasible for large numbers of redirects, in a way that just manually updating the netlify.toml isn't. Is a built-in, dynamic solution no longer supported?

@h-enk
Copy link
Member

h-enk commented Jan 9, 2024

You're right! Would surely be interesting to re-add/base upon Regis' Redirects Hugo Module with Netlify. I'm thinking of making this a Hyas integration

@h-enk h-enk reopened this Jan 9, 2024
@h-enk h-enk added the enhancement New feature or request label Jan 9, 2024
@h-enk h-enk transferred this issue from gethyas/getdoks.org Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants