Hexo 生成永久链接

在搭建Hexo博客时,文章的URL永久链接设置非常重要。好的永久链接结构不仅有利于SEO,还能提升用户体验。本文将介绍Hexo中配置永久链接的几种方法。

默认的永久链接配置

Hexo 文章链接默认的生成规则是::year/:month/:day/:title,是按照年、月、日、标题来生成的。

Hexo默认的永久链接配置位于_config.yml文件中:

1
permalink: :year/:month/:day/:title/

这种格式会生成https://mdrzf.online/2022/05/13/hello-world/,这样非常长的链接。

如果 Markdown 使用中文标题,URL转码后,将是一场灾难。

安装

安装hexo-abbrlink插件。

1
npm install hexo-abbrlink --save

修改config.yml文件中的永久链接:

1
2
3
permalink: posts/:abbrlink/ 
# 或
permalink: posts/:abbrlink.html

_config.yml配置文件下增加以下配置:

1
2
3
4
5
6
7
# abbrlink config
abbrlink:
alg: crc32 # Algorithm used to calc abbrlink. Support crc16(default) and crc32
rep: hex # Representation of abbrlink in URLs. Support dec(default) and hex
drafts: false # Whether to generate abbrlink for drafts. (false in default)
force: false # Enable force mode. In this mode, the plugin will ignore the cache, and calc the abbrlink for every post even it already had an abbrlink. (false in default)
writeback: true # Whether to write changes to front-matters back to the actual markdown files. (true in default)

生成的链接将如下所示:

1
2
3
4
5
crc16 & hex
https://post.zz173.com/posts/66c8.html

crc16 & dec
https://post.zz173.com/posts/65535.html
1
2
3
4
5
crc32 & hex
https://post.zz173.com/posts/8ddf18fb.html

crc32 & dec
https://post.zz173.com/posts/1690090958.html

hexo三连试一下效果吧

1
hexo clean && hexo generate && hexo server