I want to redirect an entire section and the child sections/pages to its renamed entity. Say I have some posts under til
section as follows:
/content/til/post1.md
/content/til/post2.md
/content/til/post3.md
which are accessible at {baseURL}/til/postN
and I want to move them to notes
section:
/content/notes/post1.md
/content/notes/post2.md
/content/notes/post3.md
and have redirects so that anyone visiting {baseURL}/til/postN
will be redirected to the new location which is at {baseURL}/notes/postN
. I realize I can use aliases
frontmatter config on each individual page so that a redirect page is generated for the old path. But, is it possible to apply aliases
somehow on the section and cascade it down the section for each page?
I tried to have the following config in the updated content/notes/_index.md
path:
---
title: "Notes"
aliases:
- /til
---
But this only generates redirect for the /til
listing and not for every post. I was hoping something like the following would work:
---
title: "Notes"
aliases:
- /til
cascade:
aliases:
- /til/:slug
---
But it doesn’t work since it seems like those special tokens don’t work with aliases
at present. Is there a different way to go on about this within Hugo itself?
I’m interested in the response to this question too, as I have handled this situation outside of Hugo (e.g. creating a redirection rule in Cloudflare), which is not great for development.
Cascades have params
and target
parameters. See here, so a working example would need to look like this at least:
---
title: "Notes"
aliases:
- /til
cascade:
params:
aliases:
- /til/:slug
---
I did not test it though…
I would also approach the issue more “globally”, one level up in content/_index.md
:
cascade:
params:
alias:
- /til/:slug
target:
path: '{/news,/news/**}'
Then again (part 1): The params
looks like only params are available, and alias is a default key in the frontmatter.
Then again (part 2): The :slug
participle might be unknown in cascades.
I know this does not help (immediately), but:
If it turns out that you can’t do that with cascades, then leave this code above in your cascade, create an output format for your redirects (Netlify has a _redirects
file for instance where you can define your, ehm, redirects) and in there you do some magic with ranging through all pages, then in each page that is based on this cascade above you will have a .Params.alias
that you can range through and that page has a .Slug
that you can use to search and replace :slug
.
Long story short, I don’t think this is an integrated feature, but with a little extra work you can get it done with a cascade instead of lots of individual files.
by looking at url mangement customize aliases i would try something like
you may use simple redirects with hugo server
.
see: Configure server
just played with the custom alias template and it does not work ootb. It is called for each file in the subfolders but it does only create one file for the main section and not the children - maybe because the alias is the same for each page…
but
I managed to get it running with a bare hugo theme site.
-
created bare hugo theme site
-
moved the /posts
section to /moved
.
-
add cascade in /moved/index.md
-
created default alias file as resource in assets
-
special alias template in layouts/alias.html
- calculate target path
- load template from assets
- execute as template to inject values
- publish to target path
git clone --single-branch -b topic-554583-cascadeAlias https://github.com/irkode/hugo-forum.git topic-554583-cascadeAlias
cd topic-554583-cascadeAlias
hugo server
now try these:
hint: without the first check in the custom alias for root section the main section redirect file was empty - no idea why.