summaryrefslogtreecommitdiff
path: root/content/blog/2024/moving-pages-in-hugo.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/blog/2024/moving-pages-in-hugo.md')
-rw-r--r--content/blog/2024/moving-pages-in-hugo.md43
1 files changed, 43 insertions, 0 deletions
diff --git a/content/blog/2024/moving-pages-in-hugo.md b/content/blog/2024/moving-pages-in-hugo.md
new file mode 100644
index 0000000..942df70
--- /dev/null
+++ b/content/blog/2024/moving-pages-in-hugo.md
@@ -0,0 +1,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.