Hugo Aliases Workaround with Netlify
Reading the Hugo documentation, aliases are supposed to be very simple and may well be for most people, but I have had trouble making them work, and while trying to resolve the issue I found other people have similar problems.
For me, it might be because of my directory structure, my permalinks setup, my use of slugs that match the directory name - all of which I am happy with and do not want to change.
My permalinks -
permalinks: event: '/talk/:slug/' tags: '/tag/:slug/' categories: '/category/:slug/' publication_types: '/publication-type/:slug/' post : '/:year/:month/:slug/' # this is different than the default
The front matter of the post -
slug: "indexing-the-works-of-shakespeare-in-elasticsearch-part-4-searching-via-web-api-in-net-5" title: "Indexing the Works of Shakespeare in Elasticsearch – Part 4, Searching via Web API in .NET 5"
As mentioned above, I don’t want to change the structure of my content directory. My slugs are important because I want the URLs of the posts to match the URLs when I was using WordPress to host my blog. I don’t want to change the titles of my posts to generate URLs that match the desired URLs.
When I moved from WordPress to Hugo, I spent a lot of time making sure that the URLs of blog posts remained the same. As with anything like this, it is easy to miss a post or two. By the time someone pointed it out to me, the new URLs had been indexed by Google, but I had LinkedIn and Twitter links to the old URL.
The new (accidental) URL -
What it should have been -
I tried aliases locally, and with tests published to Netlify, neither worked for me. I tried multiple ways of writing the alias to no avail.
But there is an easy solution.
In a previous post, I gushed about how good Netlify is. And here is another simple feature that solves this problem.
netlifty.toml file I added a redirect from the new (accidental) URL, to the one I wanted -
[[redirects]] from = "/2020/12/indexing-the-works-of-shakespeare-in-elasticsearch-part-4-searching-via-web-api-in-.net-5" to = "/2020/12/indexing-the-works-of-shakespeare-in-elasticsearch-part-4-searching-via-web-api-in-net-5" status = 301