Setting A Custom Output Format in Hugo for Netlify (or GitLab Page's) _redirects File
Tags: netlify, gitlabpages, hugo
Published: 2020-12-21 18:35:02

---
title: Setting A Custom Output Format in Hugo for Netlify (or GitLab Page's) _redirects File
published: true
date: 2020-12-21 18:30:20 UTC
tags: [netlify,GitlabPages,hugo]
canonical_url: https://codelab.farai.xyz/snippets/hugo-netlify-redirects/
---

_[Setting A Custom Output Format in Hugo for Netlify (or GitLab Page's) \_redirects File](https://codelab.farai.xyz/snippets/hugo-netlify-redirects/) was originally published on [Farai's Codelab](https://codelab.farai.xyz/)._
* * *

The`_redirects` is used to [specify redirects for Netlify](https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file). [GitLab Pages also supports redirects](https://docs.gitlab.com/ee/user/project/pages/redirects.html), but none of the fancy Netlify things like splats, non 3xx HTTP codes and the like.

While [Hugo supports custom output formats](https://gohugo.io/templates/output-formats/), it’s hard to understand the documentation which makes it work. Eventually, I came across [a commit in the Hugo Docs repo where they set up an \_redirects file](https://github.com/gohugoio/hugoDocs/commit/c1ab9894e8292e0a74c43bbca2263b1fb3840f9e).

With this and a few modifications, I was able to set up redirects. To do this:

1. Create an `outputFormat` (named <var>REDIR</var> here) for the `_redirects` file with <var>mediatype</var> `text/prs.netlify`<sup id="fnref:1"><a href="#fn:1" role="doc-noteref">1</a></sup>, <var>baseName</var> `_rediects` and set <var>isPlainText</var> and <var>notAlternative</var> to `true`.
2. Add the media type <var>mediatype</var> with an array of suffixes containing an empty string and the delimiter set to empty string as well.
3. Include the output format for the home page template alongside the other ones set<sup id="fnref:2"><a href="#fn:2" role="doc-noteref">2</a></sup>. The config file (`config.yaml` in my case) should have something like this now.

```
outputs:
home: ["HTML", "RSS", "REDIR"]

mediaTypes:
text/prs.netlify:
    suffixes: [""]
    delimiter: ""

outputFormats:
REDIR:
    mediatype: text/prs.netlify
    baseName: _redirects
    isPlainText: true
    notAlternative: true
```

4. Finally, create a template for the `_redirects` file, which should be named `index.<outputFormat_name>` (`index.redir` in my case).

```
{{ range $p := .Site.Pages -}}
{{ range .Aliases }}
{{- printf "%s %s %d" . $p.RelPermalink ($p.Params.HTTPCode | default 301) }}
{{ end -}}
{{- end -}}
```

The `($p.Params.HTTPCode | default 301)` lets you set `HTTPCode` in the front matter with the relevant [3xx redirection HTTP code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_redirection). I also put a default at `301` for [**Moved Permanantly**](https://en.wikipedia.org/wiki/HTTP_301).

And this should do it. I don’t plan to stay on GitLab Pages very long so I’ll write another post for the platform I eventually move to. I hope to support be able to make more complex redirects by then.

<section role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p>It wasn’t <code>text/prs.netlify</code> to begin with but reading about <a href="https://en.wikipedia.org/wiki/Media_type#Personal_or_vanity_tree">MIME types</a>, the <dfn>vanity tree</dfn> for <q>non publicly available products or experimental media types</q> makes sense here. It could have been <code>x.netlify</code>, but it’s discouraged. I might be overthinking this. <a href="#fnref:1" role="doc-backlink">↩︎</a></p>
</li>
<li id="fn:2" role="doc-endnote">
<p>Default for the home page is <code>["HTML", "RSS"]</code>. <a href="#fnref:2" role="doc-backlink">↩︎</a></p>
</li>
</ol>
</section>
* * *

Thanks for reading. If you liked this post, [support my work](/support/) by sharing this post, refering me for a job, sponsoring me on [Patreon](https://patreon.com/faraixyz), sending me money on [Ko-fi](https://ko-fi/faraixyz), [Paypal](https://paypal.me/fgandiya) or [Buy Me A Coffee](https://www.buymeacoffee.com/faraixyz). You could also use my affiliate links to buy something on [Amazon](https://amazon.com?tag=faraixyz03-20) or [Namecheap](https://shareasale.com/r.cfm?b=467188&u=2110247&m=46483&urllink=namecheap%2Ecom&afftrack=farai%2Exyz). Note that I may earn a commisions through those links.

Got feedback? Email me at [gandiyafarai + feedback at gmail dot com](mailto:gandiyafarai@gmail.com?body%3dWow%252C%2bthat%2bwas%2ba%2bgreat%2bpost.%2bThanks%2bfor%2bwriting%2bit%2521%2bThese%2bare%2bmy%2bgenuine%2bthoughts%2band%2btotally%2baren%2527t%2bfrom%2bthe%2bprepopulated%2btext.%26subject%3dFeedback%2bon%2bSetting%2bA%2bCustom%2bOutput%2bFormat%2bin%2bHugo%2bfor%2bNetlify%2b%2528or%2bGitLab%2bPage%2527s%2529%2b_redirects%2bFile).