Singular and plural section names in urls

Given a section like content/book it possible to configure Hugo to produce the following urls?

  • example.com/books (plural) to render my list template
  • example.com/book/1984 (singular) to render a single template

Based on what I read in the docs it seems section should always be singular at content/section and it will always be singular in urls.

Thanks

Check out Taxonomies.

I may not understand taxonomies, and I’m new to Hugo, but it seems like they create urls like example.com/tags/books (where tag is the taxonomy, and books is a tag)

It doesn’t seem like taxonomies is the answer here. Hey, @rdwatters do you have any ideas about this?

I found a solution to this:

Name your section folder in the plural (e.g. “books”) and set a “permalinks” parameter in the config to make it singular. The _index.md file doesn’t respect the permalinks parameter, so this:

content/
   - books
      - _index.md
      - book-one.md

with this setting

[permalinks]
  books = "/book/:title/"

would give you the “books” listing page, but book/book-one single page.

Feels a bit hacky, so I’d bet it’s not a long-term solution, but it appears to work.

I would think twice about usability…
If I encounter a link like …/book/xyz I would assume deleting xyz (going to the parent) will take me to the overview of all books.

1 Like

I agree, and open to suggestions about how to solve it in a less hackey way. The issue above was one of keeping links in tact from a previous site structure.

1 Like

If it’s just about legacy and only a few of that, what about just creating the content as book/… and an additional folder with just an _index.md and a template that will instead of rendering the current child pick the childs from the book folder?

I know this adds maintenance and probably duplicate code but if the url does not absolutely need to be displayed as books then what about a meta refresh, this could be a shortcode placed in the books/_index.md and inserting <meta http-equiv="refresh" content="0;url=https://your.site/book/"> in the header (see https://www.w3.org/TR/WCAG20-TECHS/H76.html) or depending on your hosting something like a 301 in a .htaccess

Yeah, one way or another there will be an extra page that needs to be dealt with and the refresh is a good idea. Thanks!

Well the extra page can only be avoided if the given hosting allows.
For legacy purpose it might be acceptable to put it as 301 (perm moves) into the .htaccess or the apache sites config.
Issue with that of course is that hugo does not know about and url clashes could happen.

1 Like