Hexo搭建之SEO

前言

既然hexo是一个博客系统,最终肯定是要发布到互联网的,而互联网核心之一便是搜索引擎,做好SEO更利于搜索引擎的收录。

配置站点URL

按照hexo默认的设置,生成的链接为如下格式,尤其是当文章的title包含中文时,生成的链接十分冗长,这不仅十分不美观不利于传播,而且不利于SEO。

1
yourdoem/:year/:month/:day/:title/

因此,推荐使用abbrlink插件来缩短链接。

安装指令:

1
2
npm install hexo-abbrlink --save
# 淘宝自改C

然后,修改站点配置文件

1
2
3
4
5
6
7
8
9
# 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
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks

为:

1
2
3
4
5
6
7
# 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
permalink: /posts/:abbrlink.html # 此处可以自己设置,也可以直接使用 /:abbrlink posts也可改也可以去掉
abbrlink:
alg: crc16 #算法: crc16(default) and crc32
rep: dec #进制: dec(default) and hex

这样在下次 hexo g 时,你的文章链接将变为 http://example.com/posts/XXXXX.html

  1. 使用该插件后,md文件的 front-matter 将出现 abbrlink: XXXXX 字段,其值对应文章的URL。

  2. 你可以在 hexo g 之前在 front-matter 中手动添加 abbrlink: XXXXX 字段指定其值使其生成已知的链接(便于记忆和其他页面的引用,如本站友链说明页面为‘99999’),但是请注意不要使不同的文章出现相同的值。

  3. 该插件可选配置已在注释表明,但建议使用示例配置。

  4. 对应站点配置文件的修改,请hexo cl 保证其修改生效,在此之前不要忘记保存对yml的修改。

配置robots

该文件用于告诉spider那些目录或文件允许被抓取,那些不允许,而配置该文件,禁止spider爬取某些文件,可以缩短爬取时间,降低服务器负载。

配置方法

yourblog/source/下新建robots.txt,内容如下:

1
2
3
4
5
6
7
8
User-agent: *
Allow: / # 允许所有目录

Disallow: /js/
Disallow: /css/
Disallow: /fonts/
Disallow: /vendors/
Disallow: /lib/

请根据自己的public目录增减条目,参考文章:

robots.txt 文件详解_passport_daizi的博客-CSDN博客

使用 hexo-neat 插件缩小资源大小

执行如下命令安装插件

1
npm install hexo-neat --save 	#淘宝源请使用 cnpm 代替 npm

_config.yml 内追加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# hexo-neat
# 说明:exclude字段为排除的文件,支持通配符,请使用半角单引号引用内容。
neat_enable: true #功能开关
# 压缩html
neat_html:
enable: true
exclude:
# 压缩css
neat_css:
enable: true
exclude:
- '**/*.min.css'
# 压缩js
neat_js:
enable: true
mangle: true
output:
compress:
exclude:
- '**/*.min.js'
- '**/jquery.fancybox.pack.js'
- '**/index.js'

配置sitemap

sitemap即网站地图,该文件格式为txt,位于网站根目录,其作用是更为直接的向搜索引擎提供站内页面的链接,

对于hexo,请执行以下命令安装sitemap插件:

1
2
3
npm install hexo-generator-sitemap --save
npm install hexo-generator-baidu-sitemap --save
# 淘宝自改C,懂?

然后使用 Hexo g 指令,并检查 public 文件夹中是否包含 sitemap.xml、sitemap.txt、baidusitemap.xml 文件。

优化媒体资源

使用速度更快的存储

建议将媒体内容存放在“对象存储”或者“内容分发网络”而不是网站目录中,除非你的服务器带宽很大,足以支持巨大的流量。

使用先进的媒体格式

若不得不在服务器上存储媒体资源,请使用更适合互联网传输的媒体格式,如

类型 建议格式
图片 webp、gif、jpg
视频 webm
音频 mp3

减少HTML中的CSS和JS

减少html中的css和js无疑是有利于SEO的,你可以将这些内容写入css文件和js文件,然后在head标签中引用它们。