summaryrefslogtreecommitdiff
path: root/content/blog/2024/moving-pages-in-hugo.md
blob: 942df704d0647c538d9fc5599b348d53209cdea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
+++
title = "Moving pages in Hugo"
author = ["Michał Sapka"]
date = 2024-10-26T00:29:00+02:00
categories = ["blog"]
draft = false
weight = 2001
image_dir = "blog/images"
image_max_width = 600
Abstract = "How to move pages without annoying everyone"
Listening = "Moonspell - Alma Matter"
+++

Moving `.html` files around is great fun, but can annoy visitors.
Linkrot is a nightmare, and `Hugo` has terrible idea on what an `guid` of a page is.

First, `RSS`.
By default, Hugo uses the `permalink` as `id` of each item.
So, when we move a file, the id changes, and RSS readers will treat it a new file.
To fix this, we need to have our own rss layout.
Grab the original one from [Microsoft GitHub](https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml) and create a new layout file `rss.xml`.
Then, adjust `channel.item.guid` (line 54 as of this moment) and put something unique there.
I use Publishing date:

```xml
<guid>{{ crypto.MD5 .Date }}</guid>
```

Now, moving pages will be invisible to RSS readers.
But then we've got the old links.
What we want is to have automatic redirects to new locations.
Hugo has it built in as [aliases](https://gohugo.io/methods/page/aliases/).
You can add `aliases` key in front matter of any page.
Hugo will create an .html file with redirect in place of each alias you define

```md
...
aliases = ["/contact/", "/me/contact/", "/blog/contact/"]
...
```

Note, that aliases work only for `single` pages.
If you want an `list` to redirect to a new location, you need to use something external, like nginx redirects.