# How to generate /section/index.html for all sections?

**URL:** <https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085>\
**Category:** support\
**Created:** [June 7, 2020, 12:31am UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085 "2020-06-07T00:31:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 7, 2020, 12:31am UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/1 "2020-06-07T00:31:13Z")

</div>

Hello,

I’m new to Hugo but not to SSGs. I have been pulling out my hair trying to get Hugo to generate an index.html under each section in /public/ and I would really appreciate some feedback.

Scenario: I have a Hugo-based documentation website with several pages organized by section (/content/section1, /content/section2, etc.). I want users to be able to click the section name in a breadcrumb navigation and have it load an index page at /public/section/index.html which lists all of the pages under that particular section. I have a full ToC on the homepage, and the navbar is generating the breadcrumb as expected, but without the index.html files, the links are 404.

Thus far, I’ve created the following templates and files without effect:

```
/themes/theme/layouts/_default/list.html
/themes/theme/layouts/_default/section.html
/themes/theme/layouts/section/index.html
/content/section1/_index.md
/content/section2/index.md

```

Regardless of what I do, Hugo will not generate an index.html page under any of the section directories in /public.

This seems like such an easy thing to accomplish, but I just can’t figure it out. I do see that section indexes are generated in the root of /public (/public/section1.html, /public/section2.html, etc.), but that’s not what I want.

I’d appreciate any feedback you can provide.

Cheers

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 7, 2020, 7:23pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/2 "2020-06-07T19:23:56Z")

</div>

Please provide a demo repo with a project showing what’s happening, and then describe what you expect. 🙂

---

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 7, 2020, 7:49pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/3 "2020-06-07T19:49:53Z")

</div>

@maiki

Thanks for the reply, but like I said this has already been identified as an existing issue ([4428](https://github.com/gohugoio/hugo/issues/4428)). Expected behaviour is that `/content/section/_index.md` should generate `/public/section/index.html`, but when `uglyurls = true` it generates `/public/section.html` instead. This leaves `/public/section/` without a directory index, which is not the traditional expectation of directory indexes regardless of pretty URLs.

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 7, 2020, 11:24pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/4 "2020-06-07T23:24:36Z")

</div>

> [@thayerwilliams](#):
>
> Expected behaviour is that `/content/section/_index.md` should generate `/public/section/index.html` , but when `uglyurls = true` it generates `/public/section.html` instead.

I’m trying to figure out why that is your expected behavior. In the docs part of the example for rendering with the `uglyURLs` option is:

```auto
└── content
    └── about
    | └── _index.md // <- https://example.com/about.html

```

Whereas sans `uglyURLs` (default), `/content/section/_index.md` generates `/public/section/index.html`. That’s what you want, it appears.

Listen, if you are okay with this having found a two year old bug report, I’m good. Please mark your message as the solution. 🙂

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 7, 2020, 11:38pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/5 "2020-06-07T23:38:54Z")

</div>

I think it finally clicked: you want most pages to resolve to `filename.html`, but not section list pages, which themselves take on the name of the the section.

This is why I asked for a demo repo, and why someone should put it together with real world examples and add it to that issue. I suspect hardly any of the regulars around here use `uglyURLs`, so how it is supposed to behave looks mostly correct from our POV. And when you ask, “how do I generate an `index.html`?” I wonder why you are using `uglyURLs` at all, since by default all of them are index pages…

---

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 7, 2020, 11:46pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/6 "2020-06-07T23:46:29Z")

</div>

No problem. If you review the bug thread I’ve linked to, you’ll see that the Hugo documentation was updated immediately _after_ the bug was discovered, but the debate continued as to whether this was appropriate behaviour as it is unconventional. Personally, it does seem odd that Hugo will not allow developers to generate an `index.html` inside a sub-directory if `uglyurls = true`.

The ticket remains open to implement an option which supports the original (expected) behaviour that was present in Hugo until sometime between versions 0.21 and 0.54, as well as the behaviour that was inadvertently adopted sometime later (so as to not break existing websites).

---

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 7, 2020, 11:50pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/7 "2020-06-07T23:50:04Z")

</div>

For what it’s worth, I’m using ugly URLs because this website will be served from a local filesystem (unhosted) and pretty URLs won’t work in such a case (e.g. you must link to the HTML file itself).

---

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 7, 2020, 11:52pm UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/8 "2020-06-07T23:52:35Z")

</div>

So this appears to be a side effect of `uglyurls = true`. When uglyurls is true, section pages are generated in the root directory of /public instead of their relative section folders. There is an open ticket to resolve this here:

> <https://github.com/gohugoio/hugo/issues/4428>
>
> Discourse thread: https://discourse.gohugo.io/t/section-index-md-page-isnt-being-created/10627
> Sample project: https://github.com/ChrisTucker2000/hugorepro
> Steps to reproduce:
> Create a section index page, e.g. /content/cats/\_index.md.
> Create an appropriate template file, e.g. /themes/mytheme/layouts/\_default/list.html.
> Set uglyURLs =...

**Workaround:** As **[TjeuKayim](https://github.com/TjeuKayim)** mentions in the above bug thread, a workaround is to name the \_index.md something else (e.g. dirlist.md) and add `slug: index` to the front matter of the file. This will generate index.md in the expected section’s directory. Depending on your theme’s design, you might need to add `layout: section` to the front matter as well to avoid it using the single.html template.

For example:

1. Rename `/content/some-section/_index.md` to `/content/some-section/dirlist.md`
2. Add `slug: index` and `layout: section` to the front matter of the file
3. You’ll also need to ensure [permalinks are configured](https://gohugo.io/content-management/urls/#permalinks) to use :slug for the affected content files
4. Hugo will now generate `/public/some-section/index.html` using the section.html layout template as expected

---

<div class="post-metadata">

**Author:** ![thayerwilliams](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/thayerwilliams/32/11566_2.png) [@thayerwilliams](https://discourse.gohugo.io/u/thayerwilliams)\
**Post date:** [June 8, 2020, 12:01am UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/9 "2020-06-08T00:01:18Z")

</div>

> [@maiki](#):
>
> I think it finally clicked: you want most pages to resolve to `filename.html` , but not section list pages, which themselves take on the name of the the section.

No, I simply want _one_ `index.html` rendered as a section list in every sub-directory containing content pages, regardless of whether uglyurls is true. This currently isn’t possible. For example, I want /public to render as follows:

```auto
└── /public
    └── /section1
    | └── article1.html
    | └── index.html <- section template listing all pages under section1
    | └── some-other-article.html
    └── /section2
    | └── article1.html
    | └── article2.html
    | └── article3.html
    | └── index.html <- section template listing all pages under section2

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [June 10, 2020, 12:01am UTC](https://discourse.gohugo.io/t/how-to-generate-section-index-html-for-all-sections/26085/10 "2020-06-10T00:01:22Z")

</div>

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