修改hexo sitemap 生成插件中的链接模板

我的博客是同一份源文件部署到GitHub Pages和Coding Pages上,并分别分配了两个域名。(给GitHub Pages分配域名是因为我的 username.github.io 获得了长城认证;给Coding Pages分配域名是担心我的 username.coding.me也同样被墙。)在 _config.yml 里面配置的url是 https://chchuj.coding.me ,而且不想改,因为想把它当成永久链接。

于是问题来了。

hexo-generator-sitemaphexo-generator-baidu-sitemap生成的站点地图里面的链接全是 https://chchuj.coding.me/(post.path)。但我想分别用来做https://chalkit.ml/https://chalkit.tk/ 的站点地图。如果直接把这样的sitemap提交给Google Search Console百度站长平台,它们是不认的,因为sitemap中的url跟网站的域名不匹配。

只要分别配置一下两个sitemap插件的模板,就能自定义sitemap中的url域名。

hexo-generator-sitemap 的模板 默认是”\blog\node_modules\hexo-generator-sitemap\sitemap.xml”,url的默认格式是:

1
<loc>{{ post.permalink | uriencode }}</loc>

修改为:

1
<loc>{{ "https://chalkit.ml/" + post.path | uriencode }}</loc>

域名要加””变成字符串,不然deploy的时候会报错

hexo-generator-baidu-sitemap的模板是”\blog\node_modules\hexo-generator-baidu-sitemap\baidusitemap.ejs”,其中url的默认格式是:

1
<loc><%- encodeURI(url + post.path) %></loc>

修改为:

1
<loc><%- encodeURI("https://chalkit.tk/" + post.path) %></loc>

域名要加””变成字符串,不然deploy的时候会报错

hexo g d之后就可以把sitemap分别提交给Google和百度了。同时可以在”\blog\source\robots.txt”里面分别指定两个sitemap文件:

Sitemap: https://chalkit.ml/sitemap.xml
Sitemap: https://chalkit.tk/baidusitemap.xml

主要参考文献:《Hexo博客之后续SEO优化

Chalkit wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!