使用hexo-abbrlink插件持久化文章链接

转载声明:本文转载自 CSDN - Hexo:hexo利用hexo-abbrlink插件生成永久链接,原作者:两年半的坤

前言

Hexo 默认的静态 URL 格式是 :year/:month/:day/:title,即按年、月、日、标题的形式生成固定链接。

例如:http://www.xxx.com/2022/11/08/文章标题

默认 URL 格式会产生很多斜杠,百度蜘蛛在抓取网页时会根据网页权重抓取网页,比如最先抓取的首页(一般都是自己的域名,比较简短),因此在链接里不宜出现过多的斜杠,最好不要超过两个

还有一个问题就是,如果文章的标题使用了中文,经过转码后,URL 会变得特别长,影响用户体验和 SEO 效果。

使用 hexo-abbrlink 插件便可解决以上出现的问题,以往修改标题会导致原始链接失效,使用这个插件后只要不更改文章中的 abbrlink 值,文章的链接就会是持久的。

操作步骤

1. 安装插件

1
npm install hexo-abbrlink --save

插件链接:hexo-abbrlink

2. 配置

修改 _config.yml 配置文件:

1
2
## permalink: :year/:month/:day/:title/
permalink: posts/:abbrlink.html ## 此处可以自己设置,也可以直接使用 :/abbrlink

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

注意:如果你是 butterfly 主题,应该都有自己创建了一个主题配置的 yml(比如 _config.butterfly.yml),以下的配置放在自定义的主题配置的 yml 文件中也是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
## abbrlink config
abbrlink:
alg: crc32 # support crc16(default) and crc32 算法
rep: hex # support dec(default) and hex 进制
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.

配置参数说明

  • alg: 算法(目前支持 crc16crc32 算法,默认值是 crc16
  • rep: 形式(生成的链接可以是十六进制格式也可以是十进制格式,默认值是十进制格式)

配置示例效果

1
2
3
4
5
6
7
8
9
10
11
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

更多配置可查看具体插件链接:hexo-abbrlink

效果对比

默认生成的 URL

默认格式:http://www.xxx.com/2022/11/08/文章标题/

问题:

  • 斜杠过多(超过 2 个)
  • 中文标题转码后 URL 过长
  • 修改标题后链接会改变

使用插件生成的 URL

插件格式:https://test.com/posts/55c6.html

优势:

  • URL 简洁明了
  • 不受标题影响,修改标题后链接不变
  • 斜杠数量少,有利于 SEO
  • 永久固定链接

总结

使用 hexo-abbrlink 插件可以为 Hexo 博客生成永久固定链接,解决默认 URL 格式冗长和中文标题导致的链接过长问题,提高 SEO 效果。只需安装插件并简单配置,就能获得简洁、稳定的文章链接。

参考链接