优化博客文章永久链接的几种方法

Hexo 博客有自己独特的文章 URL 链接方式,例如在默认 Hexo+Butterfly 中,采用 title 标题作为永久链接。

但是这种链接也有弊端,使用默认永久链接格式,会导致文章页面的默认链接相当的长,基本上都是中文链接的转义字符。

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

1.默认的链接参数

我们先来看 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进制数)

然后,我们来分析一下Hexo 博客常见的文章 URL 链接方式和实现途径,

2.第一种方法-年/月/日/时/分/秒

格式

1
/posts/年/月/日/时/分/秒.html

示例:

1
/posts/2025/04/12/10/00/00.html

这样的 url 格式中年月日都会有分隔符,生成的链接会成为一个多级目录,对于搜索引擎来说并不是很友好。有些时候也遇到博客朋友采用这种链接方式,但是少之又少,虽然对于文章发布时间很容易辨别,毕竟链接还是太长,如果想实现这种链接方式,需要修改博客根目录下的 _config.yml 文件,找到

1
2
3
# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
permalink:

将里面的连接信息修改为:

1
permalink: /posts/:year/:month/:day/:hour/:minute/:second.html

这样,假设有一篇文章 Front-matter 中的日期是 2025-04-12 10:00:00,那么这篇文章的永久链接为

1
www.example.com/posts/2025/04/12/10/00/00.html

3.第二种方法-年月日时分秒

格式

1
www.example.com/posts/年月日时分秒.html

示例

1
www.example.com/posts/20250412100000.html

这种文章的 URL 链接,实际就相当于一个按照日期排列的数字 ID,同样在博客根目录下的 _config.yml 进行中设置

1
2
3
# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
permalink:

将里面的连接信息修改为:

1
permalink: posts/:year:month:day:hour:minute:second.html

4.第三种方法 -hexo-abbrlink插件

使用 hexo-abbrlink 插件可以将 Hexo 博客生成的永久链接转化为一个固定的随机值,极大缩短了永久链接的长度。

随机值生成后对文章的标题或者时间进行任何修改, abbrlink 不会发生任何变化,非常便于维护。

格式

1
www.example.com/posts/xxxxxx.html

示例

1
www.example.com/posts/8ddf18fb.html

特别是在生成文件的过程中,如果一个 abbrlink 的值已存在,那么它会自动生成其他可用的值,下面来看安装方法

1
npm install hexo-abbrlink --save

同样在博客根目录下的 _config.yml 进行修改 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.

如果我们想生成我们预期的链接样式,大家不妨看一下下面的文件链接说明

1
2
3
4
5
6
7
8
crc16 & hex
https://test.com/posts/55c6.html
crc16 & dec
https://test.com/posts/43212.html
crc32 & hex
https://test.com/posts/6ec16a2c.html
crc32 & dec
https://test.com/posts/1521457752.html

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

1
www.example.com/posts/8ddf18fb.html

当然,我们也可以在文章的 front-matter 中手动填写 abbrlink 字段的值

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

此时永久链接格式就是

1
www.example.com/posts/hexo-permalinks.html

开源项目:https://github.com/ohroy/hexo-abbrlink

5.第四种方法-hexo-abbrlink2插件

我们可以使用 hexo-abbrlink2 插件,需要进行插件安装

1
npm install hexo-abbrlink2 --save

修改 Hexo 的配置文件的 permalink: 字段为

1
permalink: posts/:abbrlink.html

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

1
2
3
# hexo-abbrlink2
abbrlink:
start: 1000 # the first id, default 0

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

1
2
3
4
# 默认起始文章id为0的情况下
https://test.com/posts/1.html
https://test.com/posts/2.html
https://test.com/posts/3.html

自定义文章起始id为1000的情况下

1
2
3
4
#自定义文章起始id为1000的情况下
https://test.come/posts/1001.html
https://test.com/posts/1002.html
https://test.com/posts/1003.html

可以说理想很丰满,但是显示很骨感,我看了看默认开源的项目地址,里面有朋友报错,貌似没有修改,建议大家还是使用 hexo-abbrlink 插件。

开源地址:https://github.com/ohroy/hexo-abbrlink2

6.第五种方法-哈希值法

基于文件路径的哈希,这点儿我可没有测试,只是对知识的收藏。

格式:

1
/:hash(6).html

示例

1
a3b8d9.html

原理

使用文件绝对路径生成短哈希,确保文件名或内容修改不影响链接,下面来看操作,创建 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;
});

然后,修改博客根目录下的 _config.yml文件中的 permalink: 字段

1
permalink: :hash.html

需要注意的是,必须保持文件路径不变,重命名文件会导致链接变化,当然哈希冲突概率约为太低,低到可以完全可以忽略不计,如有侵权,请联系删除。

文章作者: 花生莲子粥
文章链接: https://blog.hslzz.cn/posts/5.html