Hugo: Redirect 301 Layout

If you’ve created a static site using the wonderful Hugo package and need to create some simple redirect HTML web pages then you’ll need to do the following:

  1. Create your HTML template and place it in your main layout folder.
  2. Create an archetype template file.
  3. Create a file using the archetype and fill in all the meta header information.
  4. Publish your page.

Creating a simple redirect layout in Hugo is relatively easy and can help if you are transitioning from other content providers such as WordPress where your Hugo website structure may slightly change for some posts and pages.

Let’s explore these steps in a little more detail:

1. Create HTML Template

To create a redirecting HTML page we need to create a new HTML file and folder in Hugo.

As we are creating one page to represent one redirect, we will name the file single.html and place it in a new folder which we will label redirect .

When you have done this your folder structure should look something like this:

hugo_root/
  ├── archetypes/
  │   └── default.md
  ├── content/
  │   └── posts/
  │       └── my-blog-posts.md
  ├── layouts/
  │   └── redirect/         <--- CREATE THIS FOLDER
  │       └── single.html    <--- AND CREATE THIS FILE
  ...

Barebones HTML Redirect File

The contents of the newly created single.html file should be as follows:

<!DOCTYPE html>
<html lang="{{ .Site.Language.Lang }}">
<head>
    <meta http-equiv="Refresh" content="0; url='{{ .Params.redirect }}'" />
</head>
<body></body>
</html>

Barebones with Google Analytics

<!DOCTYPE html>
<html lang="{{ .Site.Language.Lang }}">
<head>
    {{ template "_internal/google_analytics_async.html" . }}
    <meta http-equiv="Refresh" content="0; url='{{ .Params.redirect }}'" />
</head>
<body></body>
</html>

Barebones with Link in Body

<!DOCTYPE html>
<html lang="{{ .Site.Language.Lang }}">
<head>
    {{ template "_internal/google_analytics_async.html" . }}
    <meta http-equiv="Refresh" content="0; url='{{ .Params.redirect }}'" />
</head>
<body>
<!-- uncomment if you want to insert your template's header {{ partial "header.html" }} -->
<p>Please click this [link]({{ .Params.redirect }}) if you are not redirected.</p>
<!-- uncomment if you want to insert your template's footer any {{ partial "footer.html" }} -->
</body>
</html>

2. Create Archetype Template File

To make it easier for us to remember the content needed in a redirect post we will create an archetype template file with all the needed data.

Therefore, in your archetype folder create a file labelled redirect.md – this must be the same name as the folder created in layouts in the previous step.

Your folder structure for your site should look something like this:

hugo_root/
  ├── archetypes/
  │   ├── redirect.md      <--- CREATE THIS FILE
  │   └── default.md
  ├── content/
  │   └── posts/
  │       └── my-blog-posts.md
  ├── layouts/
  │   └── redirect/
  │       └── single.html
  ...

Archetype Template

Then within this file populate the contents like so:

+++
draft = true
date = {{ .Date }}
type = "redirect"
slug = "{{ .Name }}"
redirect = "https://..."
+++
CONTENT NOT RENDERED

Only insert details here if you want to enter any additional detail about the purpose of the redirect, otherwise you can just leave this portion of your file blank. 

There are several important things here to note:

  1. There needs to be a type meta tag which matches the name of the layout folder we just created.
  2. The content are of the markdown post isn’t rendered, therefore you can type whatever you want in here. Maybe even an explanation on what to do in case you forget!
  3. Remember when you’re ready to flick this redirect live change the draft from true to false or remove the draft line completely.

3. Create Redirect Page

To create a redirect page we open up our terminal, in our current Hugo website working directory and enter the command:

$ hugo new redirect/test-my-redirect.md

This should create a new file in your contents folder, such that your directory now looks like this:

hugo_root/
  ├── archetypes/
  │   ├── redirect.md
  │   └── default.md
  ├── content/
  │   ├── redirect/
  │   │   └── test-my-redirect.md   <--- HELLO WORLD!! =)
  │   └── posts/
  │       └── my-blog-posts.md
  ├── layouts/
  │   └── redirect/
  │       └── single.html
  ...

Upon opening this new file you should see something like the following:

+++
draft = true
date = 2020-05-27T16:26:14+10:00
type = "redirects"
slug = "test-my-redirect"
redirect = "https://..."
+++
CONTENT NOT RENDERED

Only insert details here if you want to enter any additional detail about the purpose of the redirect, otherwise you can just leave it blank. 

From here you can edit this file where needed, especially the redirect tag and then remove the draft tag when finished.

4. Publish

Check your redirect file is working first by loading up the hugo local server:

$ hugo server

Test the link by visiting the url: localhost/test-my-redirect

If all works well you should hopefully experience the redirect!

Conclusion

In this article we were able to learn in Hugo:

  1. How to create a custom template layout.
  2. How to create an archetype to help use the layout needed for this type of layout template.
  3. How to create a new page using the archetype and its corresponding layout.
  4. How to publish our new layout and test.

Hopefully your redirect issues in Hugo will be solved with this helpful post.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.