Repository: gohugoio/hugo (language: go)

Issue:
Sitemap index points to itself in monolingual config with defaultContentLanguageInSubdir = true
Reference: <https://discourse.gohugo.io/t/sitemap-in-single-language-website-with-en-prefix/48861>

This isn't anything new.

site configuration

```text
baseURL = 'https://example.org/'
title = 'Hugo Forum Topic #48861'
disableKinds = ['rss','taxonomy','term']
defaultContentLanguageInSubdir = true
```

content

```text
content/
└── _index.md
```

published

```text
public/
├── en/
│   ├── index.html
│   └── sitemap.xml
├── index.html
└── sitemap.xml  <-- this is a sitemapindex, but it points to itself
```

The sitemapindex should point to `https://example.org/en/sitemap.xml` instead of pointing to itself.

failing test

```go
func TestFoo(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
baseURL = 'https://example.org/'
disableKinds = ['page','rss','section','taxonomy','term']
defaultContentLanguageInSubdir = true
-- content/_index.md --
---
title: home
---
  `
	b := hugolib.Test(t, files)

	b.AssertFileExists("public/sitemap.xml", true)
	b.AssertFileExists("public/en/sitemap.xml", true)

	b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/en/sitemap.xml</loc>")
	b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
}
```


Discussion / hints:

Fix patch:
diff --git a/hugolib/site.go b/hugolib/site.go
index e7d170d0937..2275dbe8415 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -601,7 +601,7 @@ func (h *HugoSites) fileEventsContentPaths(p []pathChange) []pathChange {
 // HomeAbsURL is a convenience method giving the absolute URL to the home page.
 func (s *Site) HomeAbsURL() string {
 	base := ""
-	if len(s.conf.Languages) > 1 {
+	if len(s.conf.Languages) > 1 || s.Conf.DefaultContentLanguageInSubdir() {
 		base = s.Language().Lang
 	}
 	return s.AbsURL(base, false)


Test patch:
diff --git a/hugolib/sitemap_test.go b/hugolib/sitemap_test.go
index 6dad39fe339..a5a94c67e75 100644
--- a/hugolib/sitemap_test.go
+++ b/hugolib/sitemap_test.go
@@ -15,6 +15,7 @@ package hugolib
 
 import (
 	"reflect"
+	"strings"
 	"testing"
 
 	"github.com/gohugoio/hugo/config"
@@ -127,7 +128,7 @@ func TestParseSitemap(t *testing.T) {
 func TestSitemapShouldNotUseListXML(t *testing.T) {
 	t.Parallel()
 
-	files := `		
+	files := `
 -- hugo.toml --
 baseURL = "https://example.com"
 disableKinds = ["term", "taxonomy"]
@@ -170,3 +171,57 @@ type: sitemap
 
 	b.AssertFileExists("public/sitemap.xml", true)
 }
+
+// Issue 12266
+func TestSitemapIssue12266(t *testing.T) {
+	t.Parallel()
+
+	files := `
+-- hugo.toml --
+baseURL = 'https://example.org/'
+disableKinds = ['rss','taxonomy','term']
+defaultContentLanguage = 'en'
+defaultContentLanguageInSubdir = true
+[languages.de]
+[languages.en]
+  `
+
+	// Test A: multilingual with defaultContentLanguageInSubdir = true
+	b := Test(t, files)
+
+	b.AssertFileContent("public/sitemap.xml",
+		"<loc>https://example.org/de/sitemap.xml</loc>",
+		"<loc>https://example.org/en/sitemap.xml</loc>",
+	)
+	b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
+	b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
+
+	// Test B: multilingual with defaultContentLanguageInSubdir = false
+	files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = true", "defaultContentLanguageInSubdir = false")
+
+	b = Test(t, files)
+
+	b.AssertFileContent("public/sitemap.xml",
+		"<loc>https://example.org/de/sitemap.xml</loc>",
+		"<loc>https://example.org/en/sitemap.xml</loc>",
+	)
+	b.AssertFileContent("public/de/sitemap.xml", "<loc>https://example.org/de/</loc>")
+	b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/</loc>")
+
+	// Test C: monolingual with defaultContentLanguageInSubdir = false
+	files = strings.ReplaceAll(files, "[languages.de]", "")
+	files = strings.ReplaceAll(files, "[languages.en]", "")
+
+	b = Test(t, files)
+
+	b.AssertFileExists("public/en/sitemap.xml", false)
+	b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/</loc>")
+
+	// Test D: monolingual with defaultContentLanguageInSubdir = true
+	files = strings.ReplaceAll(files, "defaultContentLanguageInSubdir = false", "defaultContentLanguageInSubdir = true")
+
+	b = Test(t, files)
+
+	b.AssertFileContent("public/sitemap.xml", "<loc>https://example.org/en/sitemap.xml</loc>")
+	b.AssertFileContent("public/en/sitemap.xml", "<loc>https://example.org/en/</loc>")
+}