惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
博客园_首页
博客园 - 叶小钗
腾讯CDC
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
宝玉的分享
宝玉的分享
D
Docker

Dejavu's Blog

甲骨文 ARM 实例部署 Gemma 4 模型 Headscale + Tailscale 组建虚拟专用网 在 Linux 上使用 Yubikey OpenPGP 应用 BuyVM VPS 块存储挂载教程 Alpine Linux 服务器配置指南 Alpine Linux 安装 Cloudflared Docker 多容器共享中心数据库 安装 Komari 服务器监控工具 Scaleway VPS 安装 Debian Linux Debian 13 下部署 AsmBB 论坛 使用 Kopia 自动化备份服务器数据 给 Docker 启用 IPv6 支持 Netcup 服务器安装自定义 ISO 镜像 烽火 HG5582A 光猫开启桥接模式 Docker 自托管 Shlink 短链服务 部署 Obsidian LiveSync 实时同步服务指南 我的 2025 年不完全回顾 Linux 下 Intel 核显驱动配置与硬件加速 Fedora Linux 安装配置记录 2025 年优雅地自托管 RSS 服务 Woodpecker CI 和 Gitea 实现 Hugo 自动部署 Gitea/Forgejo 集成 Woodpecker CI/CD 在 Blinko 中使用 Ollama 作为 AI 供应商 Docker 部署 Gitea/Forgejo Plausible CE 启用城市级地理位置识别 Blinko 开源 AI 知识库 Docker 部署指南 Netcup 免税账号注册及购买服务器全记录 Docker 自托管 Cloudreve Pro 私有网盘服务 GiffGaff SIM 卡使用体验和注意事项 简体中文互联网在变得糟糕吗?
优化 Hexo 网站的永久链接格式
Dejavu Moe · 2020-06-07 · via Dejavu's Blog

前言

Hexo 默认的永久链接格式目录层级太复杂,深度太大,不仅不利于 SEO,而且也不美观,本文介绍一下 Hexo 下文章的永久链接优化流程

Hexo 的文章永久链接优化方式主要有两种:

  1. 免插件式

    修改 Hexo 的配置文件的 permalinks 部分,然后在文章 Markdown 文件的 front-matter 字段进行定义

  2. 插件式

    安装 hexo-abbrlink hexo-abbrlink2 插件并配置

打开 Hexo 的配置文件,找到下列字段按照注释修改

# 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  # 带协议的网站地址,如 https://www,dejavu.moe
permalink: :year/:month/:day/:title/	# 预设永久链接格式  
permalink_defaults:      # 默认永久链接
pretty_urls:             # 美化永久链接
  trailing_index: true   # 设置 false 以删除「页面」永久链接结尾的'index.html'部分
  trailing_html: true    # 设置 false 以删除「文章」永久链接结尾的'.html'部分

免插件式

文章结尾带 .html

文章结尾带 .html 更像一个静态网页,如果你更加喜欢这种格式,可以按照下面修改

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: https://dejavu.moe  # 修改此处为实际的网站地址
permalink: :permalink        # 修改此处
permalink_defaults:
pretty_urls:
  trailing_index: true	# 设置 true 以显示「页面」永久链接结尾的'index.html'部分
  trailing_html: true	# 设置 true 以显示「文章」永久链接结尾的'.html'部分

写文章的时候在 Markdown 文件的 front-matter 部分新增字段 permalink:,然后写上实际的永久链接,比如

title: 优化 Hexo 的永久链接
toc: true
permalink: /posts/hexo-permalinks.html # 举个例子
date: 2020-06-07 23:35:40

此时永久链接格式就是 https://blog.dejavu.moe/posts/hexo-permalinks.html

文章结尾不带 .html

文章结尾不带 .html 更为简洁,如果你更喜欢这种格式,可以按照下面修改

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: https://dejavu.moe  # 修改此处为实际的网站地址
permalink: :permalink        # 修改此处
permalink_defaults:
pretty_urls:
  trailing_index: false	# 设置 false 以删除「页面」永久链接结尾的'index.html'部分
  trailing_html: false 	# 设置 false 以删除「文章」永久链接结尾的'.html'部分

在文章 Markdown 文件的 front-matter 新增字段 permalink:,然后写上实际的永久链接,比如

title: 优化 Hexo 的永久链接
toc: true
permalink: /posts/hexo-permalinks/ # 注意这部分
date: 2020-06-07 23:35:40

此时永久链接就是 https://blog.dejavu.moe/posts/hexo-permalinks/

建议将字段 permalink: 字段的配置加入 Hexo 文章模板中,这样每次使用 Hexo CLI 命令新建的文章都会自动在 front-matter 中加入 permalink: 字段

打开 Hexo 根目录下的 scaffolds 文件夹中的 post.md 文件,并在 front-matter 部分新增字段 permalink: /posts/

title: {{ title }}
date: {{ date }}
updated:
categories: 
tags:
toc: true
permalink: /posts/

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

插件式

优化 Hexo 永久链接格式常用的插件有 hexo-abbrlink hexo-abbrlink2 插件

在 Hexo 根目录打开终端安装插件

# 使用 npm 安装插件
npm install hexo-abbrlink --save
# 使用 yarn 安装插件
yarn add hexo-abbrlink

修改 Hexo 的配置文件的 permalink: 字段,比如

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

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

然后在 Hexo 的配置文件里增加 hexo-abbrlink 插件的配置

# 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.

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

#crc16 & hex
https://blog.dejavu.moe/posts/66c8.html
# crc16 & dec
https://blog.dejavu.moe/posts/65535.html
# crc32 & hex
https://blog.dejavu.moe/posts/8ddf18fb.html
# crc32 & dec
https://blog.dejavu.moe/posts/1690090958.html

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

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

本文的永久链接就为 https://blog.dejavu.moe/posts/hexo-permalinks/ (文章结尾不带 .html)

https://blog.dejavu.moe/posts/hexo-permalinks.html (文章结尾带 .html)

局限性: crc16 算法生成的最大文章数量为 65535,不过这个对绝大多数人都几乎没影响,如果一个 abbrlink 的值已存在,那么它会尝试其他可用的值

hexo-abbrlink2 插件

在 Hexo 根目录打开终端安装插件

# 使用npm安装插件
npm install hexo-abbrlink2 --save
# 使用yarn安装插件
yarn add hexo-abbrlink2

修改 Hexo 的配置文件的 permalink: 字段,比如

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

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

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

# hexo-abbrlink2
abbrlink:
  start: 100 # 启起始文章id,默认为0 ,可以自定义,比如100

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

# 默认起始文章id为0的情况下
https://blog.dejavu.moe/posts/1.html
https://blog.dejavu.moe/posts/2.html
https://blog.dejavu.moe/posts/3.html

#自定义文章起始id为100的情况下
https://blog.dejavu.moe/posts/101.html
https://blog.dejavu.moe/posts/102.html
https://blog.dejavu.moe/posts/1033.html

上面的几种方法选一种即可,我更推荐使用 免插件式 方法