Customize sitemap in Hugo Website
Guide to disable or customize sitemap.xml in hugo
In this tutorial, we’ll learn how to disable or customize sitemap.xml for Hugo website.
Overview
When you generate a website using Hugo static site generator. There are few points to understand about sitemap.xml:-
- Hugo out of the box creates a sitemap.xml based on the Sitemap Protocol v0.9.
- The sitemap.xml file contains entries for these kind of pages:-
- home is website’s home page e.g.
https://example.com/
- section is all the folders in content directory
$hugo/content/*.*
- page is all the pages in content directory
$hugo/content/*.*
. - taxonomyTerm e.g. url
/categories
,/tags
- taxonomy e.g. url
/categories/*.*
,/tags/*.*
- home is website’s home page e.g.
Following is a sample sitemap.xml including all kind of pages:-
sitemap.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<!-- <kind>home</kind> -->
<loc>https://example.com/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>section</kind> -->
<loc>https://example.com/hugo/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>page</kind> -->
<loc>https://example.com/hugo/sitemap-hugo/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>taxonomyTerm</kind> -->
<loc>https://example.com/categories/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>taxonomyTerm</kind> -->
<loc>https://example.com/tags/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>taxonomy</kind> -->
<loc>https://example.com/categories/hugo/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
<url>
<!-- <kind>taxonomy</kind> -->
<loc>https://example.com/tags/hugo-defaults/</loc>
<lastmod>2020-06-04T00:00:00+00:00</lastmod>
</url>
</urlset>
Disable sitemap.xml
There are two ways to disable generating of sitemap.xml
Disable using config.toml
This approach is recommended, when you want to disable sitemap.xml permanently everytime you build your website.
Add the "sitemap"
value to the disableKinds
configuration variable in your configuration file.
config.toml
title = "Hugo example site"
baseurl = "https://www.example.com"
disableKinds = ["sitemap"]
[taxonomies]
category = "categories"
tag = "tags"
You can turn off multiple type of pages by giving comma separated values in
disableKinds
configuration.
For example, below is an example to turn off generation of sitemap.xml , RSS feed and robots.txt files all together.
config.toml
title = "Hugo example site"
baseurl = "https://www.example.com"
disableKinds = ["sitemap", "RSS", "robotsTXT"]
[taxonomies]
category = "categories"
tag = "tags"
Disable using command-line
This approach is recommended, when you want to disable sitemap.xml for a specific build of your website.
To generate a hugo website without sitemap, execute following command from terminal:
hugo --disableKinds=sitemap
To serve website in the localhost environment without a sitemap, execute following command from terminal:
hugo server --disableKinds=sitemap
You can disable multiple kind of pages like sitemap.xml , RSS feed and robots.txt by executing following command from terminal:
hugo --disableKinds=sitemap,RSS,robotsTXT
Customize sitemap.xml
Hugo has a built-in Sitemap template, but if you want to customize sitemap.xml then first of all copy and paste below hugo’s default sitemap template in layouts/_default/sitemap.xml
location:-
/layouts/_default/sitemap.xml
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range .Data.Pages }}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{ end }}
</urlset>
Now let’s do changes in above template to meet our requirement:-
Exclude taxonomy pages from sitemap.xml
This is most common use case where you want to skip taxonomy pages like category (/category/.) and tags (/tags/.) pages from sitemap.xml.
Notice that we have added a condition {{ if ne .Kind "taxonomy" }}
in default sitemap template to exclude taxonomy pages
/layouts/_default/sitemap.xml
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range .Data.Pages }}{{ if ne .Kind "taxonomy" }}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{ end }}{{ end }}
</urlset>
Exclude specific pages from sitemap.xml
This is also very common use case where you want to skip some pages from sitemap.xml.
Notice that we have added a condition {{ if ne .Params.sitemap_ignore true }}
in default sitemap template to exclude pages based on sitemap_ignore
flag
/layouts/_default/sitemap.xml
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range .Data.Pages }}{{ if ne .Params.sitemap_ignore true }}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{ end }}{{ end }}
</urlset>
Now any page, you want to exclude from sitemap.xml, add the following property in front-matter of that page
blog-page.md
---
...
sitemap_ignore: true
---
Exclude taxonomy and specific pages from sitemap.xml
Let’s combine the last two examples where you want to skip taxonomy pages and specific pages based sitemap_ignore
flag. I am using the below sitemap.xml in my own website.
Notice that we have combined the conditions of last two examples {{ if and (ne .Kind "taxonomy") (ne .Params.sitemap_ignore true) }}
/layouts/_default/sitemap.xml
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range .Data.Pages }}{{ if and (ne .Kind "taxonomy") (ne .Params.sitemap_ignore true) }}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{ end }}{{ end }}
</urlset>
Summary
In this tutorial, we’ve learned how to customize sitemap.xml for your hugo website. If you have any other requirements of sitemap.xml customization, or you are facing issue following the tutorial. Please comment, I’ll try to solve your problem as soon as possible.