本文发布于Cylon的收藏册,转载请著名原文链接~


需求

在不禁用分类的情况下,关闭对应 sitemap.xml 中的条目以优化 SEO

  • 去除所有tag
  • 去除所有分类
  • 去除 search about me 这类页面

实验步骤

sitemap 是通过 go-template 目标进行的,只要可以使用对应语法过滤了相关路径即可以排除对应的页面

首先在配置文件增加 taxonomiesExcludedFromSitemap 选项,这个选项排除了 “hugo中分类法” 中的所有子类;其次使用 if not 来排除所有对应首页面。如下列所示

本文已 PaperModX 为例,修改文件 layouts\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 (not (in .Site.Params.taxonomiesExcludedFromSitemap .Data.Plural)) (not (strings.Contains .RelPermalink "/tags")) (not (strings.Contains .RelPermalink "/posts")) (not (strings.Contains .RelPermalink "/archives")) (not (strings.Contains .RelPermalink "/search")) (not (strings.Contains .RelPermalink "/about")) }}
  <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 -}}
	{{- $weeks := div (sub now.Unix .Lastmod.Unix) 604800 -}}
	{{- $priority := sub 1 (div $weeks 10.0 ) -}}
	{{- if ge .Sitemap.Priority $priority -}}
		<priority>{{ .Sitemap.Priority }}</priority>
	{{- else -}}
		{{- if ge $priority 1.0 -}}
			<priority>1.0</priority>
		{{- else -}}	
			<priority>{{ $priority }}</priority>
		{{- end -}}
	{{- end -}}
	{{- 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>

Reference

[1] Hugo去除Sitemap中的tags

本文发布于Cylon的收藏册,转载请著名原文链接~

链接:https://www.oomkill.com/2024/07/hugo-sitemap-without-tag-page/

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」 许可协议进行许可。