跳到主要内容
  1. 所有文章/

Hexo 进行 SEO 优化的基本指南

·3446 字·约 7 分钟

个人博客已经逐渐式微,不同于私域流量,我们的网站流量更多的来自于互联网搜索引擎,动态博客有很多成熟且简单的 SEO 优化方式,而像 Hexo 这样的静态博客毕竟还是相对小众,因此本文主要介绍在刚搭建好自己的 Hexo 博客后需要做的一些 SEO 优化步骤及注意事项

修改网站信息 #

使用文本编辑器打开 Hexo 的配置文件,找到 # Site 部分,根据实际修改网站信息

# Site
title: '网站标题'
subtitle: '网站副标题'
description: "网站描述"
keywords: "网站关键词"
author: '作者名称'
language: '网站语言'      
timezone: '网站时区'   

各项参数按照 Hexo 官方文档 说明如下:

参数描述
title网站标题
subtitle网站副标题
description网站描述
keywords网站的关键词,支持多个关键词
author您的名字
language网站使用的语言
timezone网站时区, 参考 时区列表 进行设置

下面是一个网站信息写法示例:

title: "Dejavu's Blog"
subtitle: '与君初相见,犹如故人归'
description: "Dejavu's Blog,Dejavu的博客,Dejavu的日志"
keywords: "Dejavu,博客,dejavu,Blog,Hexo"
author: 'Dejavu'
language: 'zh-CN'
timezone: 'Asia/Shanghai'

优化永久链接 #

对于独立域名,不建议使用 裸域 ,建议使用 子域名,比如:

  • 建议使用: www.example.com blog.example.com
  • 不建议使用 example.com

对于 Hexo 永久链接格式优化的说明详见另一篇:优化 Hexo 的永久链接

添加网站地图 #

常用的生成网站地图插件是 Hexo 官方的 hexo-generator-sitemap,推荐使用对 SEO 更加友好的 hexo-generator-seo-friendly-sitemap 插件来生成网站地图:

# 在 Hexo 根目录打开 Terminal/Bash 安装插件
npm install hexo-generator-seo-friendly-sitemap --save
# 或者使用 yarn
yarn add hexo-generator-seo-friendly-sitemap

在 Hexo 配置文件里加入插件配置项:

sitemap:
    path: sitemap.xml
    tag: false
    category: false
参数说明
path索引网站地图路径,默认 sitemap.xml
tag网站地图是否排除标签页面
category网站地图是否排除分类页面

建议:设置 tag: falsecategory: false 将标签页面和分类页面排除在网站地图外

以我的为例,按照上面配置后本站的网站地图即为:

下面链接仅为演示,本站 2022 年 3 月份换 Hugo 静态博客框架了……

  • 索引网站地图:https://dejavu.moe/sitemap.xml

    索引网站地图

  • 文章网站地图:https://dejavu.moe/post-sitemap.xml

    文章网站地图

  • 页面网站地图:https://dejavu.moe/page-sitemap.xml

    页面网站地图

将网站地图提交到 谷歌百度必应 等站长平台后台,请注意:

网站地图类型特殊说明
sitemap.xml百度不支持
post-sitemap.xml都支持
page-sitemap.xml都支持

添加 robots.txt #

robots.txt 是存放在网站根目录下的一个纯文本文件,它可以指定搜索引擎蜘蛛只抓取指定的内容,或者是禁止搜索引擎蜘蛛抓取网站的部分或全部内容,👉 在线生成工具

然后拷贝生成的 robots.txt 文件内容,在 Hexo 的 source 目录下新建 robots.txt 文件将内容粘贴保存即可,站点部署后使用站长工具校验更新下 robots.txt 即可

nofollow 标签 #

我们使用 Hexo 官方插件 hexo-filter-nofollow 来给站外链接添加 rel="external nofollow noreferrer" 属性,从而降低 不安全搜索引擎印象不佳 的外链对网站 SEO 的影响,安装插件:

# 在Hexo目录打开Terminal/Bash
npm install hexo-filter-nofollow --save
# 或者使用yarn
yarn add hexo-filter-nofollow

在 Hexo 的配置文件中增加插件配置项:

nofollow:
  enable: true  #true/false 启用/关闭插件
  field: site   #site/post  处理全站/仅文章链接
  exclude:      #排除域名,同的子域名视为不同的域名
    - 'exclude1.com'
    - 'exclude2.com'

主动推送链接 #

hexo-submit-urls-to-search-engine 是一个 Hexo 插件,在每次运行 hexo deploy 命令后会主动推送网站更新链接到 谷歌百度必应 站长平台,从而加快搜索引擎收录速度

安装插件 #

在 Hexo 根目录打开 Terminal/Bash:

npm install --save hexo-submit-urls-to-search-engine --save
# 或者使用yarn
yarn add hexo-submit-urls-to-search-engine

获取站长平台Token #

⚓确保在 谷歌百度必应 等站长平台已经添加了你的网站

百度 #

进入网站管理,按下图所示找到资源提交接口的 Token 并记录备用

Baidu Submit

必应 #

进入网站管理,点击右上角 ⚙️ 设置,查看 API 密钥 并记录备用

Bing Submite

谷歌 #

「得益」于中国大陆互联网的特殊境况,此步骤你可能还需要合适的代理软件🤣

打开 Google Indexing API 的官方文档可以看到 使用 Indexing API 的前提条件,按照该文档完成以下步骤:

注意在 创建服务账号 时以 json 格式保存密钥,点击 创建 会自动下载密钥文件,请妥善保存后面会用到

Google Submit

点击左侧 服务账号,有个 服务账号电子邮件地址 ,复制并记录备用

Service Control

进入 Google Search Console——管理用户和权限 后台,按照图示步骤添加用户,电子邮件地址为上一步记录的 服务账号电子邮件地址

Add User

在进入 Google Search Console——旧版网站,点进你添加资源的网站,在 已验证的拥有者 下面的 电子邮件 地址中点击 添加所有者,同样添加上一步的 服务账号电子邮件地址 并继续

Add Email

插件配置 #

编辑 Hexo 的配置文件,添加 hexo-submit-urls-to-search-engine 插件的配置项,以 的配置文件为例(部分数据已脱敏)

# auto-push-urls-to-search-engine
hexo_submit_urls_to_search_engine:
  submit_condition: count #链接被提交的条件,可选值:count | period 现仅支持count
  count: 100 # 提交最新的链接个数
  period: 1000 # 提交修改时间在 1000 秒内的链接
  google: 1 # 是否向Google提交(0:否;1:是)
  bing: 1 # 是否向bing提交(0:否;1:是)
  baidu: 1 # 是否向baidu提交(0:否;1:是)
  txt_path: submit_urls.txt ## 文本文档名, 需要推送的链接会保存在此文本文档里
  baidu_host: https://dejavu.moe ## 在百度站长平台中注册的域名
  baidu_token: xxxxxxxxxxx  ## 请注意这是您的秘钥, 所以请不要把它直接发布在公众仓库里!
  bing_host: https://dejavu.moe ## 在bing站长平台中注册的域名
  bing_token: xxxxxxxxxxxxxxxxx  ## 请注意这是您的秘钥, 所以请不要把它直接发布在公众仓库里!
  google_host: https://dejavu.moe ## 在google站长平台中注册的域名
  google_proxy: http://127.0.0.1:7890 # 向谷歌提交网址所使用的系统 http 代理,填 0 不使用,以Clash为例
  google_key_file: genial-xxxxxx-xxxxxx-xxxxxxxxx.json #Google服务项目的json文件名全称,放于网站根目录(与hexo _config.yml文件位置相同),请不要把json文件内容直接发布在公众仓库里!
  replace: 0  # 是否替换链接中的部分字符串,可选值:1 | 0(0:否;1:是)
  find_what: 
  replace_with:

记得看后面的注释说明,需要注意的是 google_proxy 这个配置项,在本地推送 Hexo 更新的话需要代理,如果 Hexo 源代码托管在可以访问 Google 的集成部署平台(如:Travis CI、Vercel、Github Actions…)请将代理配置设为 0,并将部署命令由 hexo generate 改为 hexo generate && hexo deploy

如果在本地测试推送到 Google 时出现类似下面错误:

Google response:  {
  error: {
    code: 403,
    message: 'Indexing API has not been used in project 345670987654 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/indexing.googleapis.com/overview?project=1234567854 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.',
    status: 'PERMISSION_DENIED',
    details: [ [Object], [Object] ]
  }
}

这是因为没有启用 Indexing API,访问提示里面的网址链接,点击 启用 即可给予该权限即可

Enable Indexing API

如果按照上述步骤成功的话,那么在执行 hexo deploy 后,会得到类似下面的提示(本站不提交百度后台🤣),那么恭喜你,配置完成啦~

INFO  Deploying: cjh_bing_url_submitter
INFO  Deploy done: cjh_bing_url_submitter
INFO  Deploying: cjh_google_url_submitter
INFO  Deploy done: cjh_google_url_submitter
INFO  Submitting bing urls

INFO  Bing Submitting: https://dejavu.moe/posts/hexo-seo/
INFO  Bing Submitting: https://dejavu.moe/posts/canokey-basic-use/
INFO  Bing Submitting: https://dejavu.moe/posts/canokey-pigeon/
INFO  Bing Submitting: https://dejavu.moe/posts/21st-birthday/
INFO  Bing Submitting: https://dejavu.moe/posts/windows-scoop/
INFO  Bing Submitting: https://dejavu.moe/posts/no-400-computer/
INFO  Bing Submitting: https://dejavu.moe/posts/gsf-drm-certification/
INFO  Bing Submitting: https://dejavu.moe/posts/ctlist/
INFO  Bing Submitting: https://dejavu.moe/posts/telegram-flowerss/
INFO  Bing Submitting: https://dejavu.moe/posts/telegram-upload/
INFO  Bing Submitting: https://dejavu.moe/posts/hexo-blog/
INFO  Bing Submitting: https://dejavu.moe/posts/windows-terminal/
INFO  Bing Submitting: https://dejavu.moe/posts/lice-license-generator-viewer/
INFO  Bing Submitting: https://dejavu.moe/posts/cfw-tun/
INFO  Bing Submitting: https://dejavu.moe/posts/c-note-3/
INFO  Bing Submitting: https://dejavu.moe/posts/c-note-1/
INFO  Bing Submitting: https://dejavu.moe/posts/powershell-time/
INFO  Bing Submitting: https://dejavu.moe/posts/c-note-2/
INFO  Bing Submitting: https://dejavu.moe/posts/gpg-verified-github/
INFO  Bing Submitting: https://dejavu.moe/posts/windows-clion/
INFO  Bing Submitting: https://dejavu.moe/posts/windows-software/
INFO  Bing Submitting: https://dejavu.moe/posts/manjaro-note/
INFO  Bing Submitting: https://dejavu.moe/posts/onekey-dd-vps/
INFO  Bing Submitting: https://dejavu.moe/posts/hexo-permalinks/
INFO  Bing Submitting: https://dejavu.moe/posts/git-npm-yarn-proxy/
INFO  Bing Submitting: https://dejavu.moe/posts/hexo-next-beautify/
INFO  Bing Submitting: https://dejavu.moe/posts/hello-world/
INFO  Submitting google urls

INFO  Google Submitting: https://dejavu.moe/posts/hexo-seo/
INFO  Google Submitting: https://dejavu.moe/posts/canokey-basic-use/
INFO  Google Submitting: https://dejavu.moe/posts/canokey-pigeon/
INFO  Google Submitting: https://dejavu.moe/posts/21st-birthday/
INFO  Google Submitting: https://dejavu.moe/posts/windows-scoop/
INFO  Google Submitting: https://dejavu.moe/posts/no-400-computer/
INFO  Google Submitting: https://dejavu.moe/posts/gsf-drm-certification/
INFO  Google Submitting: https://dejavu.moe/posts/ctlist/
INFO  Google Submitting: https://dejavu.moe/posts/telegram-flowerss/
INFO  Google Submitting: https://dejavu.moe/posts/telegram-upload/
INFO  Google Submitting: https://dejavu.moe/posts/hexo-blog/
INFO  Google Submitting: https://dejavu.moe/posts/windows-terminal/
INFO  Google Submitting: https://dejavu.moe/posts/lice-license-generator-viewer/
INFO  Google Submitting: https://dejavu.moe/posts/cfw-tun/
INFO  Google Submitting: https://dejavu.moe/posts/c-note-3/
INFO  Google Submitting: https://dejavu.moe/posts/c-note-1/
INFO  Google Submitting: https://dejavu.moe/posts/powershell-time/
INFO  Google Submitting: https://dejavu.moe/posts/c-note-2/
INFO  Google Submitting: https://dejavu.moe/posts/gpg-verified-github/
INFO  Google Submitting: https://dejavu.moe/posts/windows-clion/
INFO  Google Submitting: https://dejavu.moe/posts/windows-software/
INFO  Google Submitting: https://dejavu.moe/posts/manjaro-note/
INFO  Google Submitting: https://dejavu.moe/posts/onekey-dd-vps/
INFO  Google Submitting: https://dejavu.moe/posts/hexo-permalinks/
INFO  Google Submitting: https://dejavu.moe/posts/git-npm-yarn-proxy/
INFO  Google Submitting: https://dejavu.moe/posts/hexo-next-beautify/
INFO  Google Submitting: https://dejavu.moe/posts/hello-world/
Bing response:  { d: null }
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/windows-clion/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/windows-clion/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:54.711856184Z'
    }
  }
}
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Bing response:  { d: null }
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/cfw-tun/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/cfw-tun/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.106851703Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/21st-birthday/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/21st-birthday/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.112713013Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/gpg-verified-github/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/gpg-verified-github/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.120954286Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/no-400-computer/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/no-400-computer/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.103282799Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/telegram-flowerss/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/telegram-flowerss/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.107897922Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/hexo-next-beautify/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/hexo-next-beautify/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.109232702Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/powershell-time/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/powershell-time/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.101591193Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/hexo-permalinks/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/hexo-permalinks/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.108845477Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/lice-license-generator-viewer/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/lice-license-generator-viewer/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.123448014Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/git-npm-yarn-proxy/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/git-npm-yarn-proxy/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.144272888Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/c-note-1/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/c-note-1/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.117114796Z'
    }
  }
}
Bing response:  { d: null }
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/ctlist/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/ctlist/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.153820168Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/manjaro-note/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/manjaro-note/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.172090178Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/c-note-3/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/c-note-3/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.151561414Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/gsf-drm-certification/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/gsf-drm-certification/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.183224805Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/hello-world/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/hello-world/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.176952007Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/windows-scoop/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/windows-scoop/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.202706149Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/c-note-2/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/c-note-2/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.191244284Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/windows-terminal/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/windows-terminal/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.177885874Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/hexo-blog/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/hexo-blog/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.213400955Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/onekey-dd-vps/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/onekey-dd-vps/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.238086937Z'
    }
  }
}
Bing response:  { d: null }
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/windows-software/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/windows-software/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.285457708Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/telegram-upload/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/telegram-upload/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.434063306Z'
    }
  }
}
Bing response:  { d: null }
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/canokey-pigeon/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/canokey-pigeon/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.506601771Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/hexo-seo/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/hexo-seo/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:55.506345107Z'
    }
  }
}
Google response:  {
  urlNotificationMetadata: {
    url: 'https://dejavu.moe/posts/canokey-basic-use/',
    latestUpdate: {
      url: 'https://dejavu.moe/posts/canokey-basic-use/',
      type: 'URL_UPDATED',
      notifyTime: '2022-01-15T11:04:56.057364034Z'
    }
  }
}

到此为止,关于 Hexo 的 SEO 优化套路就到此为止啦,以后就安心写作,等待被收录吧!收录速度个人感觉:必应 > 谷歌 > 百度

不管哪种方式,好好写作、优质的原创内容才是 SEO 最好的利器

Dejavu Moe
作者
Dejavu Moe
Not for success, just for growing.