How to omit page from sitemap

How would I go about omitting certain pages from the generated sitemap? I understand that I can create my own template to override the built in one for sitemap.xml, but is there a way to exclude certain pages?

I only ask because I’ve written a conditional script that determines whether or not my analytics should be loaded, based on whether a localstorage key & value are set in the browser. I wanted to have a page accessible on my site that I could open up before working on the site to place the key, and thus keep my browsing/development activity from polluting my analytics data.

I could just manually place the key into localstorage on each browser I use, but it’s a little messy; as few browsers allow manual addition of data without extensions or plugins, and the key-setting page would allow me to work from any computer/browser, without the need for excluding IPs (plus my home network uses a dynamic one anyway…)

Obviously, I want this page to only be accessible to me, although it’s not the end of the world if someone stumbles onto it. I just don’t want it indexed for search engines and similar.

Cheers,

Jamie.

1 Like

Add a bool flag to the page params, then check on that in the template.

1 Like

Thanks @bjornerik. Worked perfectly

For reference to others looking for the solution to this, here’s my code. I used the boolean “private” on pages I didn’t want included in the sitemap, then set my sitemap.xml layout as the following. It’s the standard, suggested layout, with the addition of my conditional check.

   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {{ range .Data.Pages }}
	  {{ if not .Params.private }}
	  <url>
	    <loc>{{ .Permalink }}</loc>
	    <lastmod>{{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ with .Sitemap.ChangeFreq }}
	    <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
	    <priority>{{ .Sitemap.Priority }}</priority>{{ end }}
	  </url>
	  {{ end }}
  {{ end }}
</urlset>

Cheers!

7 Likes

Thank you very much! I did implement your code myself and it did work.

I just used this to exclude some pages from the sitemap and it threw an error in Hugo 0.17

The culprit was the line break between {{ range .Data.Pages }} and {{ if not .Params.private }}

Eliminating this line break and also the one between {{{ end }} {{ end }} fixed everything.

This should be safeHTML. It’s probably been changed at some point.

1 Like

excuse my ignorance, but how did you set the boolean to private on the pages ?? Thanks for the help !!

It is with a simple front matter parameter: In TOML i.e.

private = true

Boolean values are set without quotes.