博客文章永久链接设置

配置文章的永久链接有利于 SEO,当然也更好看。

在站点的_config.yml中,生成的文章链接默认为:

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

这种情况下,文章名称一旦是中文,那生成的链接简直就是灾难…

1
https://xxx.com/post/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E4%BB%AC%E7%9A%84%E5%A4%A7%E8%84%91-%E6%9B%B4%E5%AE%B9%E6%98%93%E6%B2%89%E8%BF%B7%E6%B8%B8%E6%88%8F-%E8%80%8C%E4%B8%8D%E6%98%AF%E5%AD%A6%E4%B9%A0.html#more

解决方案一(已过时)

在 md 文件的 Front-matter 区域,增加一个 urlname 属性,用来放文章的英文名字。

1
2
3
4
title: hexo链接持久化终极解决之道
urlname: hexo_permalink
date: 2017-08-31 23:53:24
comments: true

然后修改 _config.yml 里的配置:

1
2
3
permalink: posts/:category/:year:month:day-:urlname.html
permalink_defaults:
urlname: index

不再详细介绍,参考:Jingbin’s Home

解决方案二

hexo-abbrlink 是一个 Hexo 插件,用于根据帖子标题生成静态帖子链接。

安装

1
npm install hexo-abbrlink --save

在站点配置文件 _config.yml 中查找代码 permalink,将其更改为:

1
2
# posts/ 部分可自行更换,我的是 :year:month:day/:abbrlink.html
permalink: posts/:abbrlink/

然后在站点配置文件中添加如下代码:

1
2
3
4
# abbrlink config
abbrlink:
alg: crc32 # 算法:crc16(default) and crc32
rep: hex # 进制:dec(default) and hex

生成后的文章样式:

1
2
3
4
5
6
7
8
9
10
11
# crc16 & hex
https://xxx.com/posts/66c8.html

# crc16 & dec
https://xxx.com/posts/65535.html

# crc32 & hex
https://xxx.com/posts/8ddf18fb.html

# crc32 & dec
https://xxx.com/posts/1690090958.html

完美解决问题!🎉

配置之后常见问题:按照给的方法添加配置后生成链接都是undefined,执行以下命令即可:

1
2
hexo clean
hexo s

参考

JingBin’s Home
Time Flying
Github - hexo-abbrlink
Github - issues