I want to redirect my Homepage to a content page

I want to redirect my projects homepage directly to a content page (or more specifically to an _index.md in a subfolder).

How can I do that without a .htaccess or other webserver dependent things?

I tried to add an alias to my target page like aliases: [/] but then hugo gives me the error “Alias “/” resolves to website root directory”.

Why is this a problem for hugo and how can I work around that?

thanks in advance

Instead of doing a redirect, you could update your homepage template, which is likely named index.html.

In there, put the content you would like to display. If it’s the same code as an existing template, then extract it out into a partial to keep things DRY.

Following @zwbetz suggestion, you can use .GetPage $var where $var can be a parameter taken from config.toml that points to a specific content page.

Okay, I tried following your advice and changed in the index.html of my theme from

{{if .Site.Home.Content }}


{{if .Site.Home.Content }}
    {{ with .GetPage "/introduction" }}{{.Render}}{{end}}

This really includes the _index.md of the introduction chapter to the main page. But it somehow screws with the layout. I guess I’m doing something wrong here.

Am I going in the right direction?

Why do you need Site.Home.Content ?

This should work on its own:

{{ with .GetPage "/introduction" }}{{.Render}}{{end}}

And how does it screw with the layout?

Didn’t test it, but try:

    {{ with .GetPage "/introduction" }}{{ .Content }}{{end}}

Is there some reason a simple meta link is not the recommended approach?

In layouts index.html

<meta http-equiv="refresh" content="0; url=/page/index.html" />

It’s just not a best practice to redirect a user on the homepage. People land in your website and the first thing that happens is that it behaves in a way that they aren’t expecting. Depending on how fast things happen, it may look weird.

Also, search engines don’t favor redirects all that much.

I think the reason using .GetPage ".introduction" .Render screws with the layout might be because it results in having nested htm, head, body tags inside the body of your index.html.

Please check if that’s the case by looking at the source of index.html in your output directory ( /public if you’re running hugo).

Either remove the html, head, body tags in the index.html of your theme, or create another view (instead of single.html) and call that by {{ with .GetPage "/introduction" }}{{.Render "_name-of-view_" }}{{end}}"

What @lenzls is asking for is not so much a redirection than just Hugo knowing X page is the homepage, the same way WordPress can recognize any page as the home page.

It can be convenient for certain projects.

Update the index.html everytime is a bit of a hassle. I did not investigate though, maybe there is a simple solutions with GetPage and baseof…


Sorry for the necroposting, but I just hit this issue and finished the idea started here into a working solution. It’s correct that putting the render directive into index.html renders into the page itself. The solution is to say in index.md that this is the toplevel stuff. I did that by naming the whole contents of baseof.html and then redefining that block in index.md. My baseof.html now looks like:

{{- block "html" . }}
    {{- partial "head.html" . -}}
        {{- partial "header.html" . -}}
        {{- partial "navigation.html" . -}}
        <div id="page">
        {{- block "main" . }}{{- end }}
        {{- partial "footer.html" . -}}
{{- end }}

And the index.md is:

{{ define "html" }}
{{ with .GetPage "/posts" }}{{.Render}}{{end}}
{{ end }}

I’d still prefer to do it differently, but this essentially shows all my “posts” on the index page - which is what I wanted. And I can even prepend some intro if I really want to.

Hope it helps somebody, cheers!