HUGO

Permalink with directory structure

I have a file under ‘cars’ in a subfolder ‘mercedes’, called ‘300-sl.md’. The path to the file is ‘/content/cars/mercedes/300-sl.md’. I would like the permalink to be: ‘/autos/mercedes/300-sl/’.

I want the following configuration:

permalinks:
  cars: autos/:filename

but this gives me the permalink ‘/autos/300-sl/’. Is it possible to have the dir (‘mercedes’) in the permalink by setting a custom permalink structure in the config? I would expect there to be a ‘:dir’ or ‘:filepath’ variable. Does this exist?

Thank you in advance.

Add to the frontmatter in 300-sl.md:

url: /autos/mercedes/300-sl

check out the docs for slug (the last part of the url) and url (the full url of a page).

@davidsneighbour Thank you!

Your solution works, but it means I have to add the url to each file. I would rather automatically read the path the file is already on. Is that also possible?

Thank you in advance.

If you create an _index.md file in each subsection…

content/cars/
├── bmw/
│   ├── 740i.md
│   ├── 760i.md
│   └── _index.md
└── mercedes/
    ├── 300-sl.md
    ├── s580.md
    └── _index.md

…you can do this in your site configuration:

[permalinks]
cars = '/autos/:sections[1]/:slug'
1 Like

@jmooring Thank you! That works for this example.

However, I am after a solution that works for more than just one level, as I have created an auto-collapsing menu that works for infinite levels of content (within a section).

If there is no way to reflect the directory structure in the permalink (and mimic the default behaviour), adding a variable like ‘:dir’ or ‘:filepath’ might be a nice feature request? What do you think?

Thank you all for your time!

Did you read the documentation?
https://gohugo.io/content-management/urls/#permalinks

:sections

the content’s sections hierarchy. New in v0.83.0 Since Hugo 0.83 you can use a selection of the sections using slice syntax : :sections[1:] includes all but the first, :sections[:last] includes all but the last, :sections[last] includes only the last, :sections[1:2] includes section 2 and 3. Note that this slice access will not throw any out-of-bounds errors, so you don’t have to be exact.

1 Like

Thank you for pointing me to that. This hack/workaround solves the problem for most users/use cases.

I still think Hugo should have a setting that enables you to mimic the default behaviour.

1 Like

This is neither a “hack” nor a “workaround.” It is a “capability” or “feature.” Words matter.

1 Like

The problem is that (when defined in the config):

there is no way to reflect the directory structure in the permalink

In my opinion using subsections IS a workaround for this problem, as a subsection is not the same as a directory structure (from what I have understood). I have understood it to be like this:

  • a folder with an _index.md file is a section or subsection
  • a folder with a index.md file is a page bundle
  • a folder without those files is a directory structure (unless it is in the root of the content dir)

I was looking for a way to reflect the third option in the permalink, not the first or the second.

Did you read the documentation?

Words matter.

Please know that I am not trying to offend or attack you. I think this functionality is genuinly flawed in Hugo. I want to ask you (or the community) if this is indeed a missing feature/strange behaviour (the default behaviour being different from the one you can configure). I love to hear your opinion on this, because I know you know everything about Hugo. Having people think along is why I asked the question.

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