Published March 4, 2022
As part of transitionining from my old wordpress site to this new one created with Hugo, I wanted to be sure to preserve any external links that pointed to URLs that may no longer exist in Hugo's content structure. In partilcular, the format of permalinks to my blog posts has changed from
/year/date/post-name to just
/post/post-name. Additionally, I had created 301 redirects from several short URLS like
/demilight to unique pages; I wanted those links to be preserved as well.
Thankfully, Hugo has some simple functionality to help with this in the form of aliases. By including a relative path in any page's front matter, Hugo will create a redirect link from that relative path to the current page.
For example, I would like users who visit
https://jeff.glass/demilight to be redirected to
https://jeff.glass/project/demilight. To do this, I simply add the following in front matter of the index.html file of the demilight project:
aliases: - /demilight/
Now, if you head to https://jeff.glass/demilight, you should be taken to the correct project page.
Since everything in Hugo is a page, including list pages and other programmatically generated content, this can be used in lots of ways. For example, with the old wordpress site, my blog posts were at
jeff.glass/blog, but Hugo puts them at
jeff.glass/post. To create this redirect, I create a
_index.md file at the root of the
post folder with the following frontmatter:
--- title: "Blog Posts" aliases: - /blog/ ---
Now, I could make these modifications programmatically (and probably should have done so when I was porting things from Wordpress to Hugo, but there are few enough of them and enough edge cases that I'm just going through and handling them by manually editting frontmatter. Like good ole XKCD 1205 says, automation just isn't always worth it.