I want to redirect my Homepage to a content page


#1

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


How to select existing page as "home" page
#2

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.


#3

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.


#4

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

{{if .Site.Home.Content }}
    {{.Site.Home.Content}}
{{else}}

to

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

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?


#5

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}}

#6

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

In layouts index.html

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

#7

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.


#8

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}}"


#9

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…