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:
- Create your HTML template and place it in your main
layout
folder. - Create an archetype template file.
- Create a file using the archetype and fill in all the meta header information.
- 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:
Barebones HTML Redirect File
The contents of the newly created single.html
file should be as follows:
|
|
Barebones with Google Analytics
|
|
Barebones with Link in Body
|
|
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:
Archetype Template
Then within this file populate the contents like so:
There are several important things here to note:
- There needs to be a
type
meta tag which matches the name of thelayout
folder we just created. - 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!
- Remember when you’re ready to flick this redirect live change the
draft
fromtrue
tofalse
or remove thedraft
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:
Upon opening this new file you should see something like the following:
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:
- How to create a custom template layout.
- How to create an archetype to help use the layout needed for this type of layout template.
- How to create a new page using the archetype and its corresponding layout.
- How to publish our new layout and test.
Hopefully your redirect issues in Hugo will be solved with this helpful post.