Assign multiple slugs to one file / page

I have a taxonomy tags, this contains (conceptionally) aliases, which are tags that have been renamed in the past.

A basic example, where tag A has been superseded by B:

/tags/b/_index.md:

---
title: B
aliases:
  - /tags/A
---
Has been renamed from "A"

This works well when resolving external (URL) requests. But one one actually want’s is something like:

---
title: B
slugs:
  - /tags/A
  - /tags/B
---
Has been renamed from "A"

That is having an alias functionality while doing the internal Hugo page construction. This would have the benefit that this internal alias would also work for constructing a term page for both tags. Is something like this already possible without duplication the term page for the old and new tag?

A bit more background: The term pages contain metadata in the front matter (as params) that need to be displayed. For further usability the resolution should also work with .GetPage.

You cannot have two slugs for the same page

read that a couple of times and I don’t really get it.

something like or I’m totally lost?

  • there’s just tag B in site frontmatter (all pages updated)
  • all processsing (taxonbomy/tags) deals with B only
  • there’s just one tag page /tags/B
  • if the the browser calls tags/B it should be available as tags/B
  • if the the browser calls tags/A (bookmarked before) it should be available as tags/A
  • publish should create both term pages
  • no alias created

use case?

Well, I’s complicated. The use case is to have entries related to renamed places.

So, the tag (term) pages should exist in both spelling language (That’s not a real translation but the name in all possible languages) variants, since I get information from the front matter (using .GetPage (printf "tags/%s" ($tag | urlize))) but the tag page itself (the one thats accesssible from the browser) should be the one that’s given by the alias.

Let’s try a current and silly example, I have a tag “Gulf of Mexico” which is now supposed to be called “Gulf of America”.

What I want is a page tags/gulf-of-america:

---
title: Gulf of America
params:
  size: 620,000 square miles
slug:
  - /tags/gulf-of-america
  - /tags/gulf-of-mexico
alias:
  - /tags/gulf-of-mexico
---

What I want is to have .GetPage give me this page for /tags/gulf-of-america and /tags/gulf-of-mexico. Having the alias is certainly not required, but the Hugo internal alias is, to get the param size. To be able to present such metadata on the top of the term list page for both possible variants.

That’s not what you want. This would be a change request so .GetPage returns multiple pages…:wink: in some/all cases.

so you need something,. where you think its .GetPage. But in fact you want to realize some target pages, link behavior, information/content shown somewhere. “USE CASE”

I still don’t know how exactly how that would look like in the target site, where you use the old, new name in your links and/or content files … Sometimes it’s good to step out of the box and thing about “what you want” without “coding in the head”

here’s a repo with three Gulfs America, Mexica and Something.
the first two are normal tags with content pages. The “Something” is an alias to America.

I adjusted the terms layout to print the aliases have a look at /gulfs/gulf-of-america after building.

git clone --single-branch -b topic-56175-aliasedTags https://github.com/irkode/hugo-forum.git gulfs
cd gulfs
hugo

and browse the site.

At best this is something you “need”, and we are done - if not elaborate on what you need/want more/differently" focusing on the relations and target pages, not the code.

2 Likes

That was my initial requirement, I’m aware that having this is possible by duplication things. My question was if this might be solvable (or if it might be worthwile to enhance the existing content model) in other ways.

.GetPage would not return multiple pages if there are multiple slugs since it’s singular.

Anyway, thanks for the example, I’ve adapted it, it’s just the other way around:
Loop through all tag pages, check all aliases and get the page where it’s matching with the one we’re looking for.

But this is quite an expensive operation, currently I’m looking into either building an index of theme or use a where clause (something like ".Aliases" "ne" nil) beforehand. It would be better if the .Site contains a list off all aliases (like there is for pages - .Site.AllPages, could be named .Site.AllAliases), this would speed things up. Having this would make it possible to narrow down to eq .Kind "term" or eq .Section "tags" to just work with this subset.

Thanks again!

Thanks again @irkode your solution made me think, it was quite easy in the end. Given that $termPath is the path that will work with .GetPage the following will do the same with aliased pages:

        {{- $termPages := where $termPages ".Kind" "eq" "term" -}}
        {{- $alias := printf "/%s" $termPath -}}
        {{- $termPages = where $termPages ".Aliases" "intersect" (slice $alias) -}}
        {{- $termPage := index $termPages 0 -}}

:slight_smile: as I said “step out of the box” may help rethinking… was just on the way to re-ask for the use case of searching an abandoned page

I’ve create a blog post on this, just in case anyone is interested:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.