Hexo博客Next主题SEO优化方法(转)

本文转自:hoxis’ blog

本文主要介绍Hexo博客的优化方法,以Google搜索为例。

添加站点地图

安装插件

需要安装两个插件来生成 sitemap 文件,前一个是传统的 sitemap,后一个是百度的 sitemap。

npm install hexo-generator-sitemap --save
npm install hexo-generator-baidu-sitemap --save

修改站点配置文件

sitemap文件添加到站点配置文件_config.yml中,并修改url字段的值,其值默认为http://yoursite.com

sitemap: 
path: sitemap.xml
baidusitemap:
path: baidusitemap.xml

url: https://hoxis.github.io

安装完成后执行hexo g即会在站点public目录下生成sitemap.xmlbaidusitemap.xml

站点地图文件

添加蜘蛛协议

在站点source文件夹下新建robots.txt文件,文件内容如下:

User-agent: *
Allow: /
Allow: /archives/
Allow: /categories/
Allow: /tags/
Allow: /resources/
Disallow: /vendors/
Disallow: /js/
Disallow: /css/
Disallow: /fonts/
Disallow: /vendors/
Disallow: /fancybox/

Sitemap: https://hoxis.github.io/sitemap.xml
Sitemap: https://hoxis.github.io/baidusitemap.xml

Allow字段的值即为允许搜索引擎爬区的内容,可以对应到主题配置文件中的menu目录配置,如果菜单栏还有其他选项都可以按照格式自行添加。

需要将https://hoxis.github.io改成自己的域名。

提交站点到 Google

打开Google Search Console,添加博客地址。

提交站点到 Google

站点验证

Google给出的推荐方法是上传HTML文件,但是不知道为什么一直验证失败,所以我选择了备用方法中的HTML 标记,将给出的元标记复制到\themes\hexo-theme-next\layout\_partials\head.swig文件中。添加后运行hexo d -g将改动提交。稍后就可以验证成功了。

your-hexo-site\themes\hexo-theme-next\layout\_partials\head.swig
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="{{ theme.android_chrome_color }}">
<meta name="google-site-verification" content="xxxxxxxxxxxxx" />
<meta name="baidu-site-verification" content="xxxxxxxxx" />

该方法是通过校验网页中<head>中的属性来进行校验的。

<html>
<head>
<meta name="google-site-verification" content="S2s6Oge-PuQt56EBO4t90FLwhL0P0faoH08iSNXe8iU" />
<title> 我的标题 </title>
</head>
<body>
网页内容
</body>
</html>

测试 robots.txt 文件

点击左侧的robots.txt测试工具,根据提示提交你的robots.txt。

注意要0错误才可以,如果有错误的话,会有提示,改正确就可以了。

测试robots.txt文件

提交站点地图

点击右上角添加/测试站点地图输入 sitemap.xml 进行测试,测试无误后再提交文件。

提交站点地图

Google抓取方式

到了最后一步,如果上方的输入框留空表示抓取首页,抓取方式可以选择桌面,智能手机等等,自行根据需要选择。填好url之后,点击抓取。

Google抓取方式

抓取完成后可能会有几种状态:完成、部分完成和已重定向等,不过无需担心,这些状态并不会影响提交。此时点击请求编入索引即可,至此博客就成功提交到了 Google,你的博客在google搜索上排名想不靠前都难了,马上上google搜索一下你的关键词和博客title测试一下吧。

Baidu主动提交链接方法

该方法可直接推送.github.io结尾的网页的链接给百度而避免百度无法爬取github中链接的问题。
该方法需要安装hexo插件,参考官方说明Hexo插件之百度自动提交链接

安装插件

  • 在Hexo根目录下,安装本插件:

    npm install hexo-baidu-url-submit --save

配置

  • 配置博客根目录下的_config.yml文件

    baidu_url_submit:
    count: 3 ## 比如3,代表提交最新的三个链接
    host: https://hoxis.github.io ## 在百度站长平台中注册的域名
    token: your_token ## 请注意这是您的秘钥, 请不要发布在公众仓库里!
    path: baidu_urls.txt ## 文本文档的地址, 新链接会保存在此文本文档里

自动提交token

  • 检查确认_config.yml中的url值与图片中host后的值一致

    # URL
    ## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
    url: https://hoxis.github.io
  • 最后修改deploy
    添加一段baidu_url_submitter

    deploy: 
    - type: git
    repo: https://github.com/hoxis/hoxis.github.io.git
    branch: master

    - type: git
    repo: https://git.coding.net/hoxis/hoxis.git
    branch: master

    - type: baidu_url_submitter

推送

之后进行部署后该插件将自动进行主动推送至百度,如图所示表示推送成功。
推送成功

网站结构自身优化

出站链接添加nofollow标签

网络爬虫会在当前页面搜索所有的链接,然后一个个查看,所以就很有可能跳到别的网站就不回来了。这个时候就需要nofollow起作用了。

nofollow标签是由谷歌领头创新的一个反垃圾链接的标签,并被百度、yahoo等各大搜索引擎广泛支持,引用nofollow标签的目的是:用于指示搜索引擎不要追踪(即抓取)网页上的带有nofollow属性的任何出站链接,以减少垃圾链接的分散网站权重。

修改footer.swig文件

路径在your-hexo-site\themes\next\layout\_partials,将下面代码中的a标签加上rel="external nofollow"属性。

{{ __('footer.powered', '<a rel="external nofollow" class="theme-link" target="_blank" href="https://hexo.io">Hexo</a>') }}
<a rel="external nofollow" class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">

修改sidebar.swig文件

路径在your-hexo-site\themes\next\layout_macro,将下面代码中的a标签加上rel="external nofollow"属性;

<a href="https://creativecommons.org/{% if theme.creative_commons === 'zero' %}publicdomain/zero/1.0{% else %}licenses/{{ theme.creative_commons }}/4.0{% endif %}/" rel="external nofollow" class="cc-opacity" target="_blank">
<a href="{{ link }}" title="{{ name }}" rel="external nofollow" target="_blank">{{ name }}</a>
<a rel="external nofollow" href="{{ link.split('||')[0] | trim }}" target="_blank" title="{{ name }}">

添加关键字

修改模板your-hexo-site\scaffolds\post.md文件,添加keywordsdescription字段,用于生成的文章中添加关键字和描述。

title: {{ title }}
date: {{ date }}
tags:
keywords:
description:
---

修改博文链接

HEXO默认的文章链接形式为domain/year/month/day/postname,默认就是一个四级url,并且可能造成url过长,对搜索引擎是十分不友好的,我们可以改成domain/postname 的形式。编辑站点_config.yml文件,修改其中的permalink字段改为permalink: :title.html即可。

#permalink: :year/:month/:day/:title/
permalink: :title.html

首页title优化

更改index.swig文件your-hexo-site\themes\next\layout

将下面这段代码:

{% block title %} {{ config.title }} {% endblock %}

改成

{% block title %} {{ config.title }} - {{ theme.description }} {% endblock %}

这时候你的首页会更符合网站名称-网站描述这习惯。

进阶,做了seo优化,把关键词也显示在title标题里,可改成

{% block title %} {{ theme.keywords }} - {{ config.title }}{{ theme.description }} {% endblock %}

注意:别堆砌关键字,整个标题一般不超过80个字符,可以通过chinaz的seo综合查询检查。