默认永久链接格式的弊端

在默认Hexo+Butterfly中,采用title标题作为永久链接

使用默认永久链接格式导致我们在访问文章页面时会发现默认的文章链接相当的长,满是中文链接的转义字符

而且如果变动了标题那么会导致永久链接也会变,对搜索引擎极其不利

Hexo官方文档中也提供了一些参数可供修改

变量 描述
:year 文章的发表年份(4 位数)
:month 文章的发表月份(2 位数)
:i_month 文章的发表月份(不含前导零)
:day 文章的发表日期 (2 位数)
:i_day 文章的发表日期(不含前导零)
:hour 文章发表时的小时 (2 位数)
:minute 文章发表时的分钟 (2 位数)
:second 文章发表时的秒钟 (2 位数)
:title 文件名称 (相对于 “source/_posts/“ 文件夹)
:name 文件名称
:post_title 文章标题
:id 文章 ID (清除缓存时不具有持久性)
:category 分类。 如果文章没有分类,则是 default_category 配置信息。
:hash 文件名(与 :title 相同)和日期的 SHA1 哈希值(12位16进制数)

第一种方法:/posts/年/月/日/时/分/秒.html

格式:/posts/年/月/日/时/分/秒.html

示例:/posts/2025/04/12/10/00/00.html

这样的url格式中年月日都会有分隔符,生成的链接会成为一个四级目录,对于搜索引擎来说并不是很友好。

  1. _config.yml中设置
1
permalink: /posts/:year/:month/:day/:hour/:minute/:second.html

这样,假设有一篇文章Front-matter中的date是2025-04-12 10:00:00,那么这篇文章的永久链接为example.com/posts/2025/04/12/10/00/00.html

当然,也可以不加/posts,在_config.yml设置permalink: /:year/:month/:day/:hour/:minute/:second.html,这样的永久链接格式就为/2025/04/12/10/00/00.html,不过从生成的链接根目录看可能有比较多的文件夹,对于有强迫症的朋友们或许不是那么友好。

第二种方法,/posts/年月日时分秒.html

格式:example.com/posts/年月日时分秒.html

示例:example.com/20250412100000.html

按照这种方法,假设有一篇文章的Front-matter中的date是2025-04-12 10:00:00,那么这篇文章的永久链接就为:example.com/20250412100000.html

操作步骤:

  1. _config.yml中设置
1
permalink: posts/:year:month:day:hour:minute:second.html

这样,假设有一篇文章的Front-matter中的date是2025-04-12 10:00:00,那么这篇文章的永久链接就为:example.com/20250412100000.html这就是我目前写这篇文章时在用的永久链接格式

第三种方法 hexo-abbrlink(推荐)

注:本方法参考文献: 优化 Hexo 网站的永久链接格式 · Dejavu's Blog ,尊重知识产权是我应尽的义务。

hexo-abbrlink插件可将 Hexo 生成的永久链接转化为一个固定的随机值,极大的缩短了永久链接的长度。随机值生成后对文章的标题或者时间进行任何修改, abbrlink 不会发生任何变化,非常便于维护。

格式:blog.hslzz.cn/posts/xxxxxx.html

示例:blog.hslzz.cn/posts/8ddf18fb.html

局限性: crc16 算法生成的最大文章数量为 65535,不过这个对绝大多数人都几乎没影响,如果一个 abbrlink 的值已存在,那么它会尝试其他可用的值

在博客根目录下打开终端安装插件

1
2
3
4
5
# 使用 npm 安装插件
npm install hexo-abbrlink --save
# 使用 yarn 安装插件
yarn add hexo-abbrlink

修改 Hexo 的配置文件的 permalink: 字段,比如

1
permalink: posts/:abbrlink.html

然后在_config.yml里增加 hexo-abbrlink 插件的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# hexo-abbrlink
abbrlink:
alg: crc32 #支持crc16和crc32算法(默认crc16)
rep: hex #支持dec和hex值(默认dec)
drafts: false #(true)Process draft,(false)Do not process draft. false(default)
# Generate categories from directory-tree
# depth: the max_depth of directory-tree you want to generate, should > 0
auto_category:
enable: true #true(default)
depth: #3(default)
over_write: false
auto_title: false #enable auto title, it can auto fill the title by path
auto_date: false #enable auto date, it can auto fill the date by time today
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 abbrlink.

默认情况下,在新建文章后,abbrlink 插件会自动使用算法生成唯一的永久链接,比如

1
2
3
4
5
6
7
8
#crc16 & hex
https://blog.hslzz.cn/posts/66c8.html
# crc16 & dec
https://blog.hslzz.cn/posts/65535.html
# crc32 & hex
https://blog.hslzz.cn/posts/8ddf18fb.html
# crc32 & dec
https://blog.hslzz.cn/posts/1690090958.html

也可以在文章的 front-matter 部分手动填写 abbrlink 字段的值

1
2
3
4
title: 优化 Hexo 的永久链接
toc: true
abbrlink: hexo-permalinks #注意这部分
date: 2020-06-07 23:35:40

此时永久链接格式就是 https://blog.hslzz.cn/posts/hexo-permalinks.html

hexo-abbrlink2 插件

在 Hexo 根目录打开终端安装插件

1
2
3
4
# 使用npm安装插件
npm install hexo-abbrlink2 --save
# 使用yarn安装插件
yarn add hexo-abbrlink2

修改 Hexo 的配置文件的 permalink: 字段,比如

1
permalink: posts/:abbrlink.html

在 Hexo 的配置文件里增加 hexo-abbrlink2 插件的配置(可选)

1
2
3
# hexo-abbrlink2
abbrlink:
start: 100 # 启起始文章id,默认为0 ,可以自定义,比如100

默认情况下,在新建文章后,abbrlink2 插件会自动使用算法生成唯一的永久链接,比如

1
2
3
4
5
6
7
8
9
# 默认起始文章id为0的情况下
https://dejavu.moe/posts/1.html
https://dejavu.moe/posts/2.html
https://dejavu.moe/posts/3.html

#自定义文章起始id为100的情况下
https://dejavu.moe/posts/101.html
https://dejavu.moe/posts/102.html
https://dejavu.moe/posts/1033.html

第四种方法:基于文件路径的哈希

格式:/:hash(6).html

示例:a3b8d9.html

原理:使用文件绝对路径生成短哈希,确保文件名或内容修改不影响链接

操作步骤:

  1. 创建文件 scripts/hash-permalink.js,填入以下内容:
1
2
3
4
5
6
7
8
9
10
const crypto = require('crypto');

hexo.extend.filter.register('before_post_render', function(data) {
const hash = crypto.createHash('sha1')
.update(data.source)
.digest('hex')
.substring(0, 6);
data.hash = hash;
return data;
});
  1. 修改_config.yml
1
permalink: :hash.html

注意事项

  • 必须保持文件路径不变(重命名文件会导致链接变化)

  • 哈希冲突概率约为1600万分之一,完全可以忽略不计