Hexo Change Permanent Link Format With Backward Compatibility

Hexo Change Permanent Link Format

Recently I would like to simplify permanent link for each post. From:

/2020/09/12/deploy-docker-on-ubuntu18-04
/2022/12/15/abplearn/sixth
To:

/deploy-docker-on-ubuntu18-04
/abplearn/sixth

If you don’t care about the search engine drop right after directly changing the permalink, you can change the config in _config.yml ,change permalink from :year/:month/:day/:title/ to :title/ directly. otherwise, please read on.

install sitemap generate to generate sitemap.txt

1
yarn add hexo-generator-sitemap

add or change config in _config.yml to get sitemap.txt

1
2
3
4
5
6
7
8
9
sitemap:
path:
- sitemap.xml
- sitemap.txt
#template: ./sitemap_template.xml
#template_txt: ./sitemap_template.txt
rel: false
tags: true
categories: true

after then , run hexo g to generate the sitemap.txt.

run python script to create html for 301

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import re
from urllib.parse import unquote

domain = "https://www.52dzd.com" # your domain
baseDir = "/Users/don/Source/hexo" # the directory that point to your hexo
sitemapTxtPath = baseDir+"/public/sitemap.txt" # sitemap.txt
outputDir = baseDir+"/source" #hexo source directory
template = '''
<!DOCTYPE html>
<html>
<head>
<title>Redirecting to new url</title>
<link rel="canonical" href="[Permalink]"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=[Permalink]" />
</head>
<body>
Redirecting to <a href="[Permalink]">new url</a>...
</body>
</html>'''

with open(sitemapTxtPath, "r") as f:
lines = f.read().splitlines() # this way the string does not contains '\n'
for line in lines:
line = unquote(line, 'utf-8')
tempPath = line.replace(domain, "") #
matchObj = re.match(
'^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\.]+)$', tempPath, re.M | re.I)
if matchObj:
# print(matchObj.group(4), end='\n')
create301HtmlDir = outputDir+tempPath
create301HtmlPath = create301HtmlDir + 'index.html'
# print(create301HtmlPath, end='\n')
# get the path without date,for 301 redirect
newPath = "/"+matchObj.group(4)
if not os.path.exists(create301HtmlDir):
os.makedirs(create301HtmlDir)
newUrl = domain + newPath
with open(create301HtmlPath, mode="w+") as w:
w.write(template.replace("[Permalink]", newUrl))
# print(newUrl, end='\n')

you can run this script on your pc,but change necessary params on top of the script

Skip Render 301 Files

Change the config in _config.yml , for skip render those html files for 301

1
2
3
4
5
6
skip_render:
- "2011/**/index.html"
- "2018/**/index.html"
- "2019/**/index.html"
- "2020/**/index.html"
- "2022/**/index.html"

Generate New File

Now , you can change permalink .

Change the config in _config.yml ,change permalink from :year/:month/:day/:title/ to :title/.

Run hexo g -d to generate new file and upload those to github.

Remove the created static file

up to one year later , or maybe half a year , those files will not be needed anymore ,please delete them directly.