Root page for a multilingual site

As part of a site migration in French and German:

  • old static site, hand-crafted 10 years ago:
    everything at the root (e.g. /contact/ and /kontakt/)
  • new site with Hugo:
    languages in directories (e.g. /fr/contact/ and /de/kontakt/)

I use links between pages via translationKey and this config:

defaultContentLanguage = “fr
defaultContentLanguageInSubdir = true

and the link between pages by translationKey

I’d like to congratulate and thank Hugo’s developers for the incredible multilingual support.

My only (small) problem is the root page. Obviously, I have 2 “roots” for each language (/fr/ and /de/), but there’s still the question of the naked domain name (/).

A redirect page to the default language is created, and that’s a good idea. But is it the best?

Firstly, can the generation of this page simply be deactivated?

Secondly, I’d like the / page to be a copy of the /en/ page rather than a link to the /en/ page. Since all my links and menus are done correctly, they’ll go where they’re supposed to. And as I’m using the canonical tag correctly, no SEO problem.

Is it possible (in pure Hugo) to copy (or generate a second identical copy of) the /fr/ page and publish it in /? I couldn’t find anything in the documentation

Of course, this is easy to do when sending via FTP or beforehand via a cp in /public/. But with Hugo only, is it possible?

NB: The starting point, as this is a migration, is the possibility of launching a reindex in Google Search Console for /, which is impossible on the default redirection page (noindex, etc.).

starting with hugo 0.140.0 there is a disableDefaultLanguageRedirect config setting

Configure Hugo | Hugo.

using a dummy empty index with a special layout should enable you to copy the default languages index file to your root

1 Like

Thank you for your quick and precise reply.

I read the whole multilingual page before asking my question. But I couldn’t find any mention of this parameter.

At home, I can’t find it with the site search either. Is this just a problem for me, or is there a bug with Algolia?


But I still can’t create a root page with Hugo, even with the default redirection turned off. It seems to me that when languages are active, Hugo just writes to the defined language directories. And that’s probably a good choice!

I could continue to simply copy the existing page as I have been doing.

But, on consideration, I think I’ll just remove the noindex from alias.html. Since canonical is correct, there won’t be any problem for SEO.

Edit: once the noindex has been removed, this page can be submitted to Google Search Console.

I checked the logs, and we ran into a limit when indexing https://gohugo.io/getting-started/configuration. No surprise given the size of that page. We’ll split this into separate pages when addressing https://github.com/gohugoio/hugoDocs/issues/1888.

In the interim, I’ve futzed with the index settings… a search for “disableDefaultLanguageRedirect” is now successful.

1 Like

yes this seems to be more tricky than I thought. All directly in /content seems to be ignored.

I managed to get it quite close with a basic example

I created /content/en/central.md containing:

+++
title = 'Home (GENERAL)'
date = 2023-01-01T08:00:00-07:00
draft = false
layout = "home"
url = "/"
+++

That way this page will be

  • rendered with the same template as the Home page.
  • published to the root as index.html

I adjusted the layouts/_default/home.html from

{{ define "main" }}
    {{ .Content }}
   {{ range site.RegularPages }}
      <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
      {{ .Summary }}
   {{ end }}
{{ end }}

to

{{ define "main" }}
   {{ if .IsHome }}
      {{ .Content }}
   {{ else }}
      {{ site.Home.Content }}
   {{ end }}
   {{ range site.RegularPages }}
      <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
      {{ .Summary }}
   {{ end }}
{{ end }}

This will take the Content of the Home page for all other pages using the home layout

That’s pretty close, next would be to adjust the baseof.html where it asks for .IsHome to do the same with this page…

not yet done… and no repo yet. but if you want I can publish this working example (maybe tomorrow)

i used the multilingual example from here Multilanguage problem - #9 by jmooring, converted it to use subfolders and then added the stuff above.

and took ideas from Use frontmatter to duplicate a page to multiple output files - #5 by jmooring

maybe @jmooring has another idea, I could thing of a deferred template for that central page…


Search just solved by @jmooring THX.

You are right, I cannot find that either. But I remembered that there was something like that with the main alias page and just went to the all configuration settings and did a page search. and I’m pretty sure it will be in the release notes for that version.

had another great FUN time digging around Hugo

Here we are:

  • content/en/global.md

    +++
    layout = "global"
    url = "/"
    +++
    
  • /layouts/page/global.html

    {{- with (templates.Defer (dict "key" "global")) -}}
       {{- with os.ReadFile "public/en/index.html" -}}
          {{- . | safeHTML -}}
       {{- else -}}
          {{- warnf "No english index.html generated" -}}
       {{- end -}}
    {{- end -}}
    

try it out:

git clone --depth 1 --single-branch -b topic-53059 https://github.com/irkode/hugo-forum topic-53059
cd topic-53059
hugo server

Awesome. Thank you for your time in finding this perfect solution.


The only mini-problem that remains, when redirection is deactivated, is that the sitemap.xml no longer exists at the root. But that’s of little importance to me.

my example had the sitemap disabled

disableKinds = ['sitemap', 'rss', 'taxonomy', 'term']

change to

disableKinds = ['rss', 'taxonomy', 'term']

and you will get the sitemaps back:

global sitemap
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  
  <sitemap>
    <loc>https://example.org/en/sitemap.xml</loc>
    
      <lastmod>2023-03-15T11:00:00-07:00</lastmod>
    
  </sitemap>
  
  <sitemap>
    <loc>https://example.org/de/sitemap.xml</loc>
    
      <lastmod>2023-03-15T11:00:00-07:00</lastmod>
    
  </sitemap>
  
</sitemapindex>

also one sitemap for each language will be generated.

if you need different thing in them, you will have to adjust the embedded template (in case incl. a condition for the global one)

but that would be a different topic

The /sitemap.xml is not generated if disableDefaultLanguageRedirect = true. But the languages sitemap.xml are.

just pushed my updated repo with the described change. Test it.

hugo v0.140.1-a9b0b95ef402a1cc70bc5386d649d947e2bcc027+extended windows/amd64 BuildDate=2024-12-23T16:26:35Z VendorInfo=gohugoio
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.23.4"
File Tree
TOPIC-53059\PUBLIC
│   favicon.ico
│   index.html
│   sitemap.xml
│
├───css
│       main.min.46acbc8e3dd4ee2457a3a77fa5ed615db6c8adb4c72e720ff162ab7998d31d53.css
│
├───de
│   │   index.html
│   │   sitemap.xml
│   │
│   └───posts
│       │   index.html
│       │
│       ├───post-1
│       │       index.html
│       │
│       ├───post-2
│       │       index.html
│       │
│       └───post-3
│               bryce-canyon.jpg
│               index.html
│
├───en
│   │   index.html
│   │   sitemap.xml
│   │
│   └───posts
│       │   index.html
│       │
│       ├───post-1
│       │       index.html
│       │
│       ├───post-2
│       │       index.html
│       │
│       └───post-3
│               bryce-canyon.jpg
│               index.html
│
└───js
        main.23cd0c7d837263b9eaeb96ee2d9ccfa2969daa3fa00fa1c1fe8701a9b87251a1.js

so maybne there’s a difference and you have a custom sitemap template or your theme, or…

1 Like

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