How to redirect section to homepage

Let’s say your site URL is http://example.com and you have a section called fruit, so http://example.com/fruit/pear.

content/fruit/pear.md

If you visit http://example.com/fruit/ you get an empty page.

Does Hugo provide a way to redirect a section to the homepage, so that http://example.com/fruit -> http://example.com?

I essentially don’t want the blank section page to be shown if possible by redirecting to the homepage.

Hello @mrsmith,

you could create a custom list.html template. If you only want to redirect from http://example.com/fruit you have to place the template at layouts/fruit/list.html. This template is only used for the content type fruit.

If you prefer to redirect users in all content types you have to place the list template under layouts/_default/list.html.

HTML allows you to redirect users with a meta tag. Place this tag in the head of the list template and the redirection should work.

Hi @digitalcraftsman,

I was able to make the redirect work, but only when I created layouts/_default/list.html.
When I put the list.html in layouts/fruit/list.html the resulting index.html in fruit/index.html is blank.

Any ideas why this doesn’t work for the fruit content type specifically?

Weird that it doesn’t work. You used the same list.html template in layouts/fruit/ that has worked in the layouts/_default/ folder, right?

Could you post a directory tree of your layouts folder?

For the layouts/fruits template folder to work, you have to specify that you want to use this template folder. In your front matter, in YAML:

type: fruit
layout: list

The type variable will make it so hugo will first look into the layouts/type folder. And it will pick the layout.html file. (here, it’ll try to pick layouts/fruit/list.html, and if it doesn’t find it, it will use layouts/_default/list.html).

Normally, you can just use the section name (the folder your content file is in), or I believe you can specify the section in the front matter - but I prefer to use type everywhere.

Good luck!

Yes same list.html just moved it from layouts/_default to layouts/fruit

├── fruit
│   ├── list.html
│   └── single.html
├── index.html
├── partials
│   ├── footer.html
│   ├── header.html
│   └── topbar.html

After browsing the docs I found a solution that I tested locally. Create a new folder section at layouts/. Now add an HTML file with the name of the content type, in our case fruit.html.

Now you should be able to redirect to specific sections.

Hi everyone,
What if you have want to do it the other way around? I.e. redirecting from http://example.com/fruit to http://example.com/

That’s what I intended with the section template. The redirect url would be {{ .Site.BaseURL }} that points to http://example.com

Sorry @digitalcraftsman, i meant from example.com to example.com/fruit. I.e. a visitor types example.com in the address bar and ends up at example.com/fruit

If I understand what you’re asking, I think this could be solved in a much easier way without using meta refresh and quasi-redirects, @mrsmith. Keep your template the same at layouts/fruit/single.html and then do the following in your config.toml:

[permalinks]
  fruits = "/:title/"
# content/fruit/pears.md => mysite.com/pears/

Sorry for my late reply. This absolutely works for redirecting specific sections. Thank you.

@rdwatters this doesn’t work for me.

Also to clarify I was looking to redirect from mysite.com/fruit/ => mysite.com not mysite.com/fruit/pear => mysite.com/pear.

@mrsmith Ah, got it. So basically you just wanted a list page for the fruit content type at mysite.com/fruit/. Here is the cascade (as taken from the docs):

/layouts/section/SECTION.html
/layouts/_default/section.html
/layouts/_default/list.html
/themes/THEME/layouts/section/SECTION.html
/themes/THEME/layouts/_default/section.html
/themes/THEME/layouts/_default/list.html

Sorry for the confusion, but maybe the permalinks is a handy thing to know in the future. Cheers.

Sure thing :wink: