• Homeright=arrow
  • Blogright=arrow
  • How to Setup Sitemap for Hugo Site
featureImage

How to Setup Sitemap for Hugo Site

A sitemap serves as a digital roadmap for search engine bots, guiding them through the various pages and content on your website. This, in turn, boosts your site's search engine optimization (SEO) and aids in faster indexing. In this guide, we'll cover everything about configuring a sitemap for your Hugo website.

Synopsis

When you generate a website using Hugo, the static site generator, it generates a sitemap by default. The sitemap follows the sitemap protocol v0.9. The sitemap.xml file contains entries for various types of pages, ensuring that search engines can efficiently crawl and index your website's content. The following types of pages you might find in a sitemap.xml file for a Hugo website, along with their corresponding URLs:

⦿  Home (Website's Home Page):

URL: / or https://example.com/

Description: The root URL of your website, representing the homepage where visitors typically start their journey.

⦿  Sections (Folders in the Content Directory):

URL: /section/ (Replace section with the actual folder name)

Description: Each folder in your Hugo content directory corresponds to a section on your website. These sections may contain multiple content pages.

⦿  Pages (Content Pages in the Content Directory):

URL: /section/page/ (Replace section and page with the actual folder and file names.)

Description: These are individual content pages within each section, providing specific information or resources to your audience.

⦿  Taxonomy Term Pages (e.g., Categories, Tags):

URL: /categories/ or /tags/

Description: These pages serve as archives for content categorized under specific topics or tags. Visitors can explore content grouped by category or tag.

⦿  Taxonomy Pages (e.g., Category or Tag Pages for Specific Terms):

URL: /categories/term/ or /tags/term/ (Replace term with the actual category or tag name.)

Description: These pages display content associated with a particular category or tag, providing a focused view of related materials.

Here is a sample sitemap.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/</loc>
<lastmod>2023-09-28T08:00:00+00:00</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://www.example.com/about/</loc>
<lastmod>2023-09-20T12:00:00+00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://www.example.com/blog</loc>
<lastmod>2023-09-15T14:30:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://www.example.com/blog/post-1/</loc>
<lastmod>2023-09-10T10:15:00+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://test-hugosite.netlify.app/categories/</loc>
<lastmod>2023-09-10T10:15:00+00:00</lastmod>
</url>
<url>
<loc>https://test-hugosite.netlify.app/tags/</loc>
<lastmod>2023-09-10T10:15:00+00:00</lastmod>
</url>

<!-- Add more URL entries for additional pages as needed -->
</urlset>

Configure Sitemap

You can configure the Hugo sitemap default values by updating the config.toml file. Here's how you can do it:

[sitemap]

changeFreq = "" # Default change frequency (empty string)
filename = "sitemap.xml" # Sitemap file name
priority = -1 # Default priority (-1 means omitted from rendered sitemap)

ChangeFreq

How often a page might be changed will be set using ChangeFreq. The default value is an empty string. You can adjust the changeFreq to one of the valid values, such as "always", "hourly", "daily", "weekly", "monthly", "yearly", or "never," if you want to override the default value.

Filename

The name of the generated file. The default value is sitemap.xml.

Priority

You can also set the priority of a page. To do that, set the priority to a value between 0.0 and 1.0. The default value is -1.

Custom Sitemap Template

Hugo has a built-in sitemap template. However, you can modify the template. The following steps will show you, the steps to customize Sitemap:

Create the Sitemap:

Hugo's default sitemap template is typically located in the theme you are using. It should be in a path like themes/your-theme-name/layouts/_default/sitemap.xml or layouts/_default/sitemap.xml . If there is not any sitemap.xml file in the themes/your-theme-name/layouts/_default/ or  layouts/_default/ folder, just create one.

Copy the Default SItemap Template:

Copy the following codes and paste them into the themes/your-theme-name/layouts/_default/sitemap.xml or layouts/_default/sitemap.xml file.


{{ 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 .Permalink -}}
<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.LanguageCode }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.LanguageCode }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{- end -}}
{{ end }}
</urlset>

Customize the SitemapTemplate:

Open the copied sitemap.xml file in a text editor and make the desired customizations.

Regenerate Your Site:

After making changes to your custom Sitemap template, regenerate your Hugo site using the Hugo command to apply the custom template:

hugo

Your customized sitemap will now be generated according to the modifications you made to the sitemap.xml file in the layouts/_default directory of your Hugo project. This approach allows you to have full control over the appearance and content of your sitemap.

Disable Sitemap Generation

To disable the generation of a sitemap in Hugo, you can simply add the following line of code in the config.toml file:

disableKinds = ['sitemap']

To sum up, integrating a well-structured sitemap into your Hugo website is a fundamental strategy for improving its SEO and user experience. By facilitating efficient content indexing, sitemaps boost discoverability on search engines and enhance navigation for visitors. This guide has provided the necessary steps to create and configure a sitemap, ensuring that your Hugo website is thoroughly optimized for search engine crawlers and, ultimately, better positioned for success in the digital landscape.

Yet confused about where to start? Look no further than StaticMania! Our dedicated team of experts will guide you through the entire process, from obtaining and setting up your Sitemap to customizing your site to make it SEO-friendly. Contact us now to get started.

Generate Table of Content
thumbnail

How To Generate Table of Content For Your Blogs in Hugo

It provides readers with a quick overview of your content's structure and allows them to navigate effortlessly through your articles. Hugo, a fast and flexible static site generator, makes it easy to generate dynamic TOCs for your content. In this guide, we'll explore how to use Hugo's built-in capabilities to create and customize the table of contents for your web pages.

Read article
footer-particlefooter-particlefooter-particlefooter-particlefooter-particle
back-to-top