Aliases vs Header Redirects

Which is the recommended (or) better option to redirect internal links? (Header redirects here means the header file served by Netlify, Cloudflare Pages, etc).

You can use a combination of Hugo aliases and your host’s redirects format. Set up a custom output format for redirects based on the format your host requires. Then loop through your pages and their aliases in the corresponding template and add any manual redirects below that if needed. Disable Hugo aliases to pass the responsibility of redirects onto your host. The aliases are still available via page context with disableAliases enabled (see docs). This way, if you ever switch hosts your redirects are still accessible via Hugo aliases.

2 Likes

As @wjh18 said you can use something like this

index.redir

#### Netlify redirects from aliases
{{ range $p := site.Pages -}}
{{ range .Aliases }}
{{  . | printf "%-35s" }}	{{ $p.RelPermalink }} 301!
{{ end -}}
{{- end -}}

#### Redirect for plausible.io
{{ partial "plausible_redirects_netlify.html" . }}

config.toml

[outputFormats]
[outputFormats.REDIR]
mediatype = "text/netlify"
baseName = "_redirects"
isPlainText = true
notAlternative = true
1 Like

Thank you all for your tips. I studied both and decided to go with the header file for Cloudflare pages since it was easier to implement. But I will save this for future use.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.