Change the root of a taxonomy

I’m a bit lost in Hugo’s templating rules. I have blog posts under /content/blog/ so they appear at <base_url>/blog/... - which is what I want.

Is it possible to have tags also nested under blog, i.e. the term page would then be at <base_url>/blog/tags/ and list pages at <base_url>/blog/tags/<my_tag>/ ?

Try setting the tags permalink in your config.toml

[permalinks]
  tags = "/blog/tags/:title/"

Thanks @zwbetz, it worked out for the list pages, however not for the terms page, it’s still at ROOT/tags/. Should I then add an _index.md and set a URL manually, or is there a better way?

I’ve sorted it out. The only changes required were:

  1. Set tags = "/blog/tags/:title/" in the Permalinks section of the site config file
  2. Create a file /content/blog/tags/_index.md with the following front matter:
    title: Blog tags
    type: tags
    layout: terms
    # The below creates a redirect at /tags to this page.
    # Not really required but neat
    aliases:
        - /tags
    
  3. Use .Site.Taxonomies.tags instead of .Data.Terms in the terms template (/layout/tags/terms.html)
  4. In order for aliases from the front matter to work, you need to disable default page generation at /tags because it will overwrite the redirect page otherwise. The simplest way to achieve that I managed to find is to create an empty template file /layouts/tags/empty.html and configure the page at /tags to use it by creating /content/tags/_index.md with the following content:
    ---
    layout: empty
    ---
    

Phew.

2 Likes

Good deal. And for clarity for future readers: Point (1) sets the permalink of individual tags pages. Points (2) and (3) set the permalink of the tags list page.

1 Like

There’s still one problem though. I see that tag terms and lists get now generated in both /tags/ and /blog/tags/, effectively duplicating all pages. Also peculiar that all links on both terms pages point at the correct tags under /blog/tags/.

How can I get rid of this duplication?

Delete you public dir then rebuild. The old files are hanging around.

Thanks! You’re right, after a clean rebuild all list pages are gone.

Yet, /tags/index.html is still there. Can I disable it or otherwise turn it into a redirect page?

I’m not sure. Maybe others on here can help you.

I think I’ve figured that out. I’ve updated my solution post.
On a side note, IMHO it would be nice to have a simpler/cleaner way of reconfiguring taxonomy URLs.