Use Aliases to Redirect Old URLs

This article provides a quick guide to using Hugo aliases to redirect old URLs when reworking already published content.

Having published an article, after a while, you often need to return to the already published material and revise it. During such revision, not only the content of the article may change, but also its title. So, whenever the title changes, it’s advised to update the slug of the published page.

Here I want to emphasize the importance of having a concise, descriptive, and relevant article title and, consequently, a matching slug because they greatly help convey the page’s information to visitors and search engines.

For example, given the URL for this post is, it can be split into the following parts:

section: article/
slug: use-aliases-to-redirect-old-urls/

As per the Hugo documentation, by default, the slug value is determined by the name of the content file, for example, To produce a slug, you can use an online slugifier like

Slugify Title

Thus, whenever the name of the content file changes, the URL of the corresponding page also changes. Therefore, to prevent 404 errors for old URLs, you should add the aliases variable to the revised article’s front matter. This variable is defined as:

an array of one or more aliases (e.g., old published paths of renamed content) that will be created in the output directory structure

In my case, the article “Automate Index Upload to Algolia Search” has been revised twice. And every time, its title changed. After the first revision, the title changed to “Automate Data Import to Algolia.” During the second revision, I re-defined the title as “Automate Data Upload to Algolia Index.” With each revision, the corresponding content file name was also updated, which in turn altered the URL of the page.

Hence, to make the previously published URLs functional, I added the following relative to the base URL aliases in the article’s front matter:

    - /article/automate-index-upload-to-algolia-search/
    - /article/automate-data-import-to-algolia/

But once the aliases are specified, how does it work under the hood? As you can see, for each alias entry, Hugo created a directory that contains an auto-generated index.html file:

List Directories

For example, index.html files in the public/article/automate-data-import-to-algolia and public/article/automate-index-upload-to-algolia-search directories are identical and contain the canonical URL and the redirect target for the latest version of the article:

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

The meta http-equiv="refresh" line is responsible for the actual redirect. Also, the <meta name="robots" content="noindex"> line prevents engine bots from crawling and indexing the new alias page.

So, as you can see, Hugo provides an easy-to-apply and straightforward mechanism to handle redirecting to old URLs when reworking or migrating existing published content.

Continue reading the series “Building Your Blog, the Geeky Way”:

comments powered by Disqus