]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Add config option disableDefaultLanguageRedirect
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 12 Dec 2024 14:42:17 +0000 (15:42 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 12 Dec 2024 20:47:03 +0000 (21:47 +0100)
Fixes #13133

config/allconfig/allconfig.go
hugolib/config_test.go
hugolib/site_render.go

index b158d8a90bd86d71864952a6f7fc67ad215b5c37..be4480f45d2beba270e2a1b1627b4f0a66eb291d 100644 (file)
@@ -505,6 +505,9 @@ type RootConfig struct {
        // Set this to true to put all languages below their language ID.
        DefaultContentLanguageInSubdir bool
 
+       // Disable generation of redirect to the default language when DefaultContentLanguageInSubdir is enabled.
+       DisableDefaultLanguageRedirect bool
+
        // Disable creation of alias redirect pages.
        DisableAliases bool
 
index c9db0d2f0080bf0e6cb8b2e1d9bc6250dee76eb4..c0bfde37da42ed6c3c139366e4f355c3b4ab620e 100644 (file)
@@ -1339,6 +1339,29 @@ Home.
        b.Assert(len(b.H.Sites), qt.Equals, 1)
 }
 
+func TestDisableDefaultLanguageRedirect(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+defaultContentLanguageInSubdir = true
+disableDefaultLanguageRedirect = true
+[languages]
+[languages.en]
+title = "English Title"
+[languages.sv]
+title = "Swedish Title"
+-- layouts/index.html --
+Home.
+
+
+`
+       b := Test(t, files)
+
+       b.Assert(len(b.H.Sites), qt.Equals, 2)
+       b.AssertFileExists("public/index.html", false)
+}
+
 func TestLoadConfigYamlEnvVar(t *testing.T) {
        defaultEnv := []string{`HUGO_OUTPUTS=home: ['json']`}
 
index 83f2fce8971227884b0d9dc39f31bf050173bb1d..e5b2b62ab715839b0d9be3fc6ba0b06939d5b422 100644 (file)
@@ -334,6 +334,9 @@ func (s *Site) renderAliases() error {
 // renderMainLanguageRedirect creates a redirect to the main language home,
 // depending on if it lives in sub folder (e.g. /en) or not.
 func (s *Site) renderMainLanguageRedirect() error {
+       if s.conf.DisableDefaultLanguageRedirect {
+               return nil
+       }
        if s.h.Conf.IsMultihost() || !(s.h.Conf.DefaultContentLanguageInSubdir() || s.h.Conf.IsMultilingual()) {
                // No need for a redirect
                return nil