Redirecting Hosted Hugo Pages

Posted on Feb 16, 2021

Sometimes you want to restructure your site, or rename a page and the URL changes. For the integrity of the internet, to help all the people who linked to your site and for SEO you want to set up a redirect from the old location to the new location.

This is a pretty standard task, but how to do it is less clear when you are using Hugo hosted on to of GitLab pages, or Netlify. Hugo has a couple ways to redirect pages, one using the config/development/server file and one using aliases . GitLab Pages and Netlify both use public/_redirects for their redirects, but this public is a folder that is intentionally not committed because it is where Hugo builds all of the HTML files that make up your site.

If you try to use the config/development/server redirect method it will work when you run your site locally, but have no effect when you run your site on GitLab, or Netlify. Fortunately the aliases method results in Hugo creating an HTML redirect file at the original URL , so it works both locally and in production.

This method is easy to implement. On your .md file update the header to include aliases.

---
aliases:
	 - /original-url/
	 - /2010/01/01/earlier-url.html
---

These aliases should be relative URLs (everything after your domain name). If you are using TOML for your page metadata instead of YAML you can see an example of how to implement this here.