Hexo进阶-生成永久链接格式

永久链接

Hexo 默认的永久链接格式目录层级复杂,深度大,不仅不利SEO,也不美观!

  • Hexo 默认的永久链接配置为:year/:month/:day/:title/,这样生成的链接会成为一个四级目录,对于搜索引擎来说并不是很友好。
  • 这里介绍一个 Hexo 插件 hexo-abbrlink,它能将 Hexo 生成的永久链接转化为一个固定的随机值,极大的缩短了永久链接的长度。
    一旦生成一个随机值,之后对文章的标题或者时间进行任何修改,这个随机的 abbrlink 是不会发生任何变化的,也为 Hexo 的维护提供了便利。
  • HEXO默认的文章链接形式为domain/year/month/day/postname,默认就是四级url,并且可能造成url过长,对搜索引擎是不太不友好

安装配置

1
npm install hexo-abbrlink --save

2、配置.config.xml

  • 以下设置均修改博客根目录下的配置文件

  • 修改默认的永久链接参数

    我们可以改成domain/postname 的形式。编辑站点的_config.yml文件,修改其中的permalink字段改为permalink: :title.html即可

    1
    2
    3
    4
    5
    6
    7
    8
    # URL
    ## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
    url: https://blog.lansoo.com # 修改此处为实际的网站地址
    permalink: :permalink # 修改此处
    permalink_defaults:
    pretty_urls:
    trailing_index: false # 设置 false 以删除「页面」永久链接结尾的'index.html'部分
    trailing_html: false # 设置 false 以删除「文章」永久链接结尾的'.html'部分
  • 配置文件的 permalink: 字段,比如

    1
    permalink: posts/:abbrlink/  #文章结尾不带 .html

或者

1
permalink: posts/:abbrlink.html  #文章结尾带 .html

3、hexo-abbrlink插件配置

  • 在 Hexo 的配置文件-config.xml里增加
    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.

4、参数说明

url: 这是你博客的网址。例如,如果你使用GitHub Pages服务,你需要将这个值设置为你的GitHub Pages的URL。

permalink: 这是文章的永久链接格式。:abbrlink.html 是使用 hexo-abbrlink 插件生成的短链接格式。

permalink_defaults: 这个部分通常用来定义永久链接的默认参数。

pretty_urls: 这个部分用来定义URL的美化设置。

trailing_index: 设置为 true 时,访问 /index.html 会重定向到不带 index.html 的URL。

trailing_html: 设置为 true 时,访问 .html 文件会重定向到不带 .html 的URL。

abbrlink: 这是 hexo-abbrlink 插件的配置部分。

alg: 指定生成短链接时使用的算法,可以是 crc16 或 crc32。

rep: 指定算法结果的表示方式,可以是 dec(十进制)或 hex(十六进制)。

drafts: 是否对草稿进行处理。

auto_category: 自动根据文章所在的目录结构生成分类。

enable: 是否启用自动分类。

depth: 分类的最大深度。

over_write: 是否覆盖已有的分类。

auto_title: 是否启用自动标题。

auto_date: 是否启用自动日期。

force: 是否启用强制模式,忽略缓存。

格式生成

  • 默认情况下,在新建文章后,abbrlink 插件会自动使用算法生成唯一的永久链接,比如
    1
    2
    3
    4
    5
    6
    7
    8
    #crc16 & hex
    https://blog.lansoo.com/posts/66c8.html
    # crc16 & dec
    https://blog.lansoo.com/posts/65535.html
    # crc32 & hex
    https://blog.lansoo.com/posts/8ddf18fb.html
    # crc32 & dec
    https://blog.lansoo.com/posts/1690090958.html

配置完成后,重新部署hexo

1
hexo cl; hexo g; hexo d

Hexo免插件的办法

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

    1
    2
    3
    4
    5
    6
    ---
    title: 优化 Hexo 的永久链接
    toc: true
    abbrlink: hexo-permalinks #注意这部分
    date: 2023-08-08 22:22:22
    ---
  • Hexo 根目录下的 scaffolds 文件夹中的 post.md 文件,并在 front-matter 部分新增字段 permalink: /posts/

1
2
3
4
5
6
7
title: {{ title }}
date: {{ date }}
updated:
categories:
tags:
toc: true
permalink: /posts/

每次新建文章后在 /posts/ 后加上自定义的格式就行了,这里的 /posts/ 就是我想对所有文章的一个固定路径,相当于所有文章都是在 /posts/ 文件夹下,当然也可以把它改成你喜欢的其他的路径.

我个人还是比较喜欢无脑自动生成模式