hexo+yilia添加URL持久化


URL持久化

參考:你的Hexo博客SEO優化了嗎?

我們可以發現hexo默認生成的文章地址路徑是 【網站名稱/年/月/日/文章名稱】。比如這篇博客:
http://localhost:4000/2019/11/10/hexo+yilia添加URL持久化
這種鏈接對搜索爬蟲是很不友好的,第一它的url結構超過了三層,太深了。第二使用了中文路徑。這樣會導致一個問題,在某些聊天工具或分享鏈接的時候會造成url轉碼變成很長一串難以讀寫的鏈接。而且如果你的頁面之前被收錄或被轉載後,當你再次編輯過後可能會造成之前的url失效帶來不必要的404, 比如下面這樣:
http://localhost:4000/article/faa5d14。別擔心,我們有好辦法來解決它。

第一種方式:

打開_config.yml配置文件,找到permalink

#permalink: :year/:month/:day/:title/ 註釋掉
# 改爲下面這樣
permalink:   /article/:title.html

上面這種方式是去掉了年月日,保持網站最多三層。

第二種方式:(推薦)

安裝 hexo-abbrlink插件:

npm install hexo-abbrlink --save

配置_config.yml,查找permalink,修改成這樣:

# permalink: :title/  將之前的註釋掉
permalink: article/:abbrlink.html
abbrlink:
  alg: crc32  # 算法:crc16(default) and crc32
  rep: hex    # 進制:dec(default) and hex

安裝後的網址:http://localhost:4000/article/c5f6d65b.html

注意:配置成功需要重新生成文件hexo clean,hexo g,不然會出現undefined的問題,如下:

$ npm install hexo-abbrlink --save
npm WARN [email protected] requires a peer of eslint@>= 4.12.1 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ [email protected]
added 15 packages from 13 contributors and audited 26333 packages in 27.509s
found 21 vulnerabilities (8 low, 6 moderate, 6 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details

yansheng@MECHREVO MINGW64 /h/Hexo (blog)
$ hexo s
(node:1612) [DEP0061] DeprecationWarning: fs.SyncWriteStream is deprecated.
INFO  Start processing
INFO  Generating Baidu urls for last 100 posts
INFO  Posts urls generated in baidu_urls.txt
https://yansheng836.github.io/archives/undefined.html
https://yansheng836.github.io/archives/undefined.html
https://yansheng836.github.io/archives/undefined.html
https://yansheng836.github.io/archives/undefined.html
https://yansheng836.github.io/archives/undefined.html
https://yansheng836.github.io/archives/undefined.html

INFO  Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

弊端

其實使用這個也有弊端:

  • 按照原格式,發佈後,在github倉庫中你能很清楚的看到你寫博客的日期,因爲是按照網址分文件夾的。使用該插件後就看不到了。
    file

分類和標籤路徑名英文化

上面我們已經實現將文章的URL簡短化和英文化了,但是如果我們的標籤和分類有中文,這個插件是控制不了的,

如有個生活的標籤,該標籤下的所有文章彙總如下:http://localhost:4000/tags/生活/,還是會出現中文,其實這個問題hexo官方自帶解決方法,詳見:https://hexo.io/zh-cn/docs/configuration.html#%E5%88%86%E7%B1%BB-amp-%E6%A0%87%E7%AD%BE

截個圖:
image

站點的配置文件有category_maptag_map配置,即分類和標籤的別名(映射),舉個例子:

# Category & Tag
default_category: uncategorized
category_map:
    編程: programming
tag_map:
    生活: life

這樣標籤生活的訪問網址爲:http://localhost:4000/tags/life/,分類同理。

參考:https://yq.aliyun.com/articles/8607?spm=0.0.0.0.TP5ICj


文章首發於:hexo+yilia添加URL持久化

發佈了132 篇原創文章 · 獲贊 149 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章