Hexo-永久链接

本文最后更新于 2025年7月22日 下午

引言

打开 项目根目录\package.json 文件即可看到当前 hexo 版本

请查看版本信息,以确保此文章内容适用于你的项目

1
2
3
"hexo": {
    "version": "7.3.0"
  }

默认配置

打开 Hexo配置文档可以查看 _config.yml 各配置的功能

在使用 hexo 框架搭建博客后,其 URL 部分默认配置如下

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

url: http://example.com 部分正常修改为自己网址即可

而 permalink 默认配置过于冗长,且一旦更改文件名,文章的 URL 就会改变,不利于搜索引擎收录和外部引用

因此多数人会调整配置或安装插件来实现永久链接

永久链接

Hexo 文档

按照 hexo 官方文档永久链接(Permalinks) | Hexo 调整 Permalink

文档中记录了所有变量,以及对应的功能

用户可以根据自己的喜好调整

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

例如上述配置将 URL 设置为了纯数字形式的文章创建时间

插件

通过安装插件,如 GitHub - ohroy/hexo-abbrlink: create one and only link for every post for hexo 来实现永久链接

类似的插件有很多,根据自己的需求选择即可

安装

在项目根目录右键打开命令行工具或 Git Bash

执行命令安装

1
npm install hexo-abbrlink --save

修改 config. Yml 文件中的 permalink 为

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

注意

修改 URL 可能会造成图片无法正常显示的问题,详细信息请浏览Hexo-图片引用问题 - MiYingRuShi


Hexo-永久链接
https://mifazhan.top/posts/33b326d2/
作者
米应如是
发布于
2025年5月13日
更新于
2025年7月22日
许可协议