Hexo - 站点地图 Sitemap

如何给 Hexo 网站增加站点地图(Sitemap)?

什么是 Sitemap

Sitemap(站点地图)是一种结构化文件,用于向搜索引擎和用户清晰呈现网站内的所有重要页面、文件及彼此间的关联。它通常以 XML 格式为主(也有 HTML、TXT 等其他形式),包含网页的 URL 链接、更新时间、优先级、更改频率等元数据。他的主要作用在于帮助搜索引擎更高效地抓取和索引网站内容。

站点地图需遵循特定协议规范(如 Google Search Console、Bing Webmaster 等平台的规则),并通过搜索引擎工具提交,以辅助网站的搜索引擎优化(SEO)工作。

Hexo Sitemap

安装插件 hexo-generator-sitemap :

npm install hexo-generator-sitemap --save

在Hexo网站根目录创建模版文件:

(1)sitemap_template.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in posts %}
<url>
<loc>{{ post.permalink | uriencode }}</loc>
{% if post.updated %}
<lastmod>{{ post.updated | formatDate }}</lastmod>
{% elif post.date %}
<lastmod>{{ post.date | formatDate }}</lastmod>
{% endif %}
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
{% endfor %}
<url>
<loc>{{ config.url | uriencode }}</loc>
<lastmod>{{ sNow | formatDate }}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
{% for tag in tags %}
<url>
<loc>{{ tag.permalink | uriencode }}</loc>
<lastmod>{{ sNow | formatDate }}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.2</priority>
</url>
{% endfor %}
{% for cat in categories %}
<url>
<loc>{{ cat.permalink | uriencode }}</loc>
<lastmod>{{ sNow | formatDate }}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.2</priority>
</url>
{% endfor %}
</urlset>

(2)sitemap_template.txt:

1
2
3
4
5
{% for post in posts %}{{ post.permalink | uriencode }}
{% endfor %}{{ config.url | uriencode }}
{% for tag in tags %}{{ tag.permalink | uriencode }}
{% endfor %}{% for cat in categories %}{{ cat.permalink | uriencode }}
{% endfor %}

修改Hexo根目录的配置文件 _config.yml:

1
2
3
4
5
6
7
8
9
10
## Sitemap
sitemap:
path:
- sitemap.xml # 生成的XML文件名
- sitemap.txt # 生成的TXT文件名
template: ./sitemap_template.xml
template_txt: ./sitemap_template.txt
rel: false # 是否添加rel="sitemap"标签
tags: true # 是否包含标签页
categories: true # 是否包含分类页