HUGO

Permalinks Configuration: Ignore Top Level Section

I apologize for such a basic question but I can’t seem to find an answer. I have the following permalink config:

[permalinks]
    posts = "/post/:title/"
    pages = "/:title/"

This works great for top level files so content/pages/about.md or content/pages/page1.md. It seems to stop working for subdirectories / subfolders and I end up having to add pages to the URL for subfolders. For example of if I’ve got content/pages/dir1/file.md I’d expect the URL to end up being /dir1/file/ with the permalink config I’ve got but it’s actually ending up /pages/dir1/file/. Is there some setting to tell Hugo to use the pages permalink config for all subfolders? Thank you!

Not tested, but I suppose that at the top level _index.md of a section you could use front-matter cascade to set for example type = "pages" in all descendant content files, so that the permalink configuration kicks in.

Thanks for getting back so quickly.

In content/pages/_index.md I’ve got:

title: “Index”
date: 2020-11-16T12:20:31-06:00
draft: false
type: “pages”

And that doesn’t appear to work on Hugo v0.77.0 via hugo serve -D -w. Thanks again.

As I said above please have a look at front-matter cascade:

So that the type is inherited by all descendants.

Thank you I did miss the cascading part. It still however is not working for me. I’ve now got (content/pages/_index.md):

---
title: "Index"
date: 2020-11-16T12:20:31-06:00
draft: true
type: "pages"
cascade:
    type: "pages"
---

Top level pages work fine but I can’t get /pages/dir1/file/ or /dir1/file/ to work. I tried a type of "page" as well.

Can you please share the project so that either me or the others in this forum can have a look at the full context?

It’s not currently published and the template I’m working on is using a purchased theme but let me see if I can get it put somewhere. It’s essentially a hugo new theme <theme> though.

This isn’t the code I’ve been working with thus far but it does reproduce the issue: https://github.com/robhenley/hugo-issue

Please remove draft: true from all content files and then the pages permalink configuration should work out-of-the-box e.g. http://localhost:1313/four/

A draft is not published hence no permalink. Also once you do the above you may try removing the cascade front-matter, since it shouldn’t be needed.

So the -D flag is not honored (as in hugo serve -D -w)? The original end goal was for the URL for “four” (content/pages/dir1/four.md) to be /dir1/four/. I can set a URL it just seems the wrong way to solve the problem. I set all the pages to draft: false and there is zero desired change that I can see. I tried with cascade and without.

I’m going to take a break.

Right. Now I see -and it seems that I missed part of the original question-.

In the project config you have set:
pages = "/:title/"

Therefore with title: Four the permalink will end up being published as /four/ and not /dir1/four/.

I am afraid that you cannot achieve the latter because there is no way to ignore the top section i.e. pages and only output the nested section i.e. dir.

Also if the /:sections/ variable is used in the permalinks configuration it will output the full hierarchy.

See: https://gohugo.io/content-management/urls/#permalink-configuration-values

The -D flag is honored when a site is generated by calling hugo -D. I didn’t use it while testing the repo, so ignore what I posted above.

Your issue is about ignoring the top level section and that’s just not possible.

Thank you @onedrawingperday! I appreciate you hanging in there.

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