Using Hugo as a redirect service

I have been building my website with Hugo since early 2021. I love the control it gives me. I recently wanted to start using short URLs in presentations, that would link to either a longer URL on the website or to somewhere else altogether.

It turns out Hugo makes this easy but not entirely obvious, so I thought I would write it up.


For local redirects, use aliases. For external redirects, use this template.

Local redirects

These are the easy ones. Hugo supports aliases in a page’s front matter or header, which generate a redirect page. It took a while to find them; it would be great if they called them redirects!

I recently moved all my old blog posts to be top-level pages rather than structured by date, so a URL like /2022/02/10/some-post/ became simply /some-post/.

To prevent any links to the old structure failing, I introduced an alias to each page that looks like this:


title: Some Post
- "/2022/02/10/some-post/"

This causes Hugo to render a page at the old address that looks like this:

<!DOCTYPE html>
<html lang="en-GB">
    <link rel="canonical" href="">
    <meta name="robots" content="noindex">
    <meta charset="utf-8">
    <meta http-equiv="refresh" content="0; url=">

This establishes a permanent redirect to the new location and means that I don’t “break the web”. I like that they think about things like canonical links, and stop the old page being indexed (the robots meta tag).

Shaving some yaks

Of course I was not going to do this by hand for around 100 posts, so I wrote something to extract the date from the post and insert the alias line. It turned out that many of the older posts had a Unix-style date format, like Sat Oct 21 11:56:44 BST 2023, so naturally I felt compelled to write a little programme to convert all of these to ISO yyyy-mm-dd dates so I could generate the aliases.1

Then I realised that I also needed to know the name of the file that Hugo had rendered the page to, which determines its URL. Rather than try to reproduce Hugo’s permalink mangling logic for title / filename / slug / date etc., I cheated! I rendered the website, then searched for the file containing the post’s title: <meta property="og:title" content="Some Post" />

This all took a few hours’ fun hacking in Go, test-driven of course, and ended up with a single-use utility that would fix the date, figure out the page URL and drop the alias into the file for each post.

Shortening URLs

So now I had my mechanism. I’m using the template /go/shortcut, so on a slide, I might have to point to my Rule of 63 post at

At this point, I no longer need TinyURL or a vanity domain to point to long URLs on my own site.

External redirects

These are a little trickier, but we can effectively use the same mechanism. Hugo uses an internal template to render the redirect page. For some reason, the alias template is not documented, but it is there.

You can set a custom template for a page by giving it a type property. Hugo will look for the template in layouts/[type]/single.html. The disadvantage with copying an internal template like this is that you lose any updates to the template in future versions of Hugo, which I was not happy about.

The internal alias template uses the .Permalink property of the page object, which it turns out is not editable. You can customise the part after the site’s base URL, but nothing more. Then I realised that I could just pass a custom dictionary into the template, so my final template is a single line!

The solution


{{- template "_internal/alias.html" (dict "Permalink" -}}

and the source page now looks like this:

type: redirect

which could not be much simpler.

  1. While writing this up, I discovered that zsh has a builtin strftime function, which might have been much quicker! ↩︎