Hexo Blog optimization: Customizing post urls. The following is just a personal solution.

The cause of

The thing is, when I was sharing my interview today, I posted the link in the group and found that the URL was too ugly after escaping. QAQ searched the URL optimization of Hexo on the Internet and found this method and briefly recorded it.

When Hexo generates the article, it generates the URL based on the file name, and the file name is Chinese, so the URL is escaped, and the escaped URL looks very lengthy as shown in the figure above.

Solution search

First of all, the official reference configuration permalink (Permalinks) | Hexo configuration items in the known variables can be used as follows:

variable describe
:year Year of publication (4 digits)
:month Month of publication (2 digits)
:i_month The month in which the article was published (remove the leading zero)
:day Date of publication of the article (2 digits)
:i_day The date of publication of the article (minus the leading zero)
:hour Hours (2 digits) when the article was published
:minute Minutes (2 digits) when the article was published
:second Seconds (2 digits) when the article was published
:title Relative to “source/_posts/” Folder
:name The file name
:post_title The article title
:id The article ID (not persistent across cache reset)
:category Classification. If the article is not classified, yesdefault_categoryConfiguration information.
:hash SHA1 hash of filename (same as :title) and date (12-hexadecimal)

You can adjust the default values of variables in permalink_defaults:

url: https://ysx.cosine.ren/      # I bind the domain url
root: /
# permalink: :lang/:title/
permalink: :lang/:link/
permalink_defaults:
  lang: cn
Copy the code

Of course, you can use dates, categories, IDS, or hashes as urls if you want, but there are several problems with that

  • Date, classification and so on are set, SEO is not friendly

    • Use date to do the URL, because baidu’s search engine crawl web pages: search engines think that for small and medium-sized sites in general, 3 layers enough to bear all the content, so often grab the content is the first three layers, and more than three layers of content that the content is not important, so not often climb. For this reason permalink should be followed by no more than two slashes.
  • Use algorithms to generate a unique hash or ID for each article

    • This method is often used in some large content publishing platforms, such as CSDN article publishing ID, B station video BV number, etc., which can simply manage a large amount of content without repetition, but the generated URL is not intuitive, and a good URL is best known by name

For personal blog posts, it is best that the URL of the post roughly reflects the content of the post, so manually setting a URL suffix for each post is a more appropriate method in my opinion.

Solving steps

In the hexo configuration file _config.yml, add the following configuration:

url: https://ysx.cosine.ren/      # I bind the domain url
root: /
# permalink: :lang/:title/
permalink: :lang/:link/
permalink_defaults:
  lang: cn
Copy the code

In the future when writing blog, you can directly add the link in the article, as follows:

---
title: Hexo blog URL optimization
link: hexo-url-optimization # article actual link is: https://ysx.cosine.ren/cn/hexo-url-optimization/
catalog: true
lang: cn
date: The 2022-04-14 21:18:56 
subtitle: The blog URL escaped too long.
header-img: /img/header_img/galaxy-ngc-3190-wallpaper-for-2880x1800-60-653.jpg
tags:
- The front end
- hexo
categories:
- tool
---
Copy the code

Add the following to the scaffolds directory, post.md, draft.md, and set link to the default title of the article to avoid forgetting:

---
title: {{ title }}
link: {{ title }}
subtitle:
date: {{ date }}
catalog: true
header-img: /img/header_img/galaxy-ngc-3190-wallpaper-for-2880x1800-60-653.jpg
tags:
- The front end
- JavaScript
categories:
- notes
---
Copy the code

All you need to do now is change the value in the link attribute to the beautified name you want