Hexo博客搜索引擎优化

发布时间 2023-08-14 04:18:44作者: eagle.supper

1. 如何让自己的博客网站被收录

1.1. 首先检查自己的站点有没有被收录

以百度为例

  • 打开百度http://www.baidu.com
  • 在收索框输入site: 你的网站域名
    • 如果你用site:高级搜索没有搜索到你的网站,那么说明你的网站并没有被搜索引擎收录。
    • 如果搜索到了,但通过网站的关键词搜索不到,说明你的网站排名靠后,正需要SEO优化。

1.2. 让百度收录你的网站

  • 首先,进入百度搜索资源平台

  • 打开平台后,点击用户中心,点击站点管理,如果未登录,需要先登录;

  • 点击添加网站,选择你的协议头 http/https 按照自身情况选择,输入你的网站地址,点击下一步;

  • 验证网站: 一般验证网站有以下几种验证方法:

    1. 文件验证 (推荐)

      • 文件验证是指将百度给你的html文件放到source目录下(即source/baidu_verify_code_xxxx.html)

      • 值得注意的是:文件验证的文件是一个HTML文件,hexo可能会将他与主题镶嵌在一起,所以要在百度给你的HTML文件上加入以下代码:

        ---
        layout: false
        ---
        
    2. html标签验证

      • html标签验证则是在head标签里添加百度所指定的代码
    3. CNAME验证 (推荐)

      • CNAME验证就是添加域名解析记录

        请将code-xxxxx.guoapeng.github.io使用CNAME解析到ziyuan.baidu.com

        完成操作后请点击“完成验证”按钮。

        为保持验证通过的状态,成功验证后请不要删除该DNS记录

    对于hexo的博客来说,我个人建议CNAME和文件验证这两种

  • 网站验证成功后,就可以在站点管理管理你的网站了。

    提交完你的网站后,不出意外的情况下,搜索引擎会在几个星期内收录你的网站,一般不超过一个月,超过一个月的话可能就有问题了。

1.3. 让Bing收录你的网站

  • 首先,进入bing webmaster tools

  • 进入bing webmaster tools时,如果未登录,需要先登录;

  • 点击添加网站,输入你的网站地址,点击添加;

  • 验证网站: 一般验证网站有以下几种验证方法:

    1. xml文件验证 (推荐) 文件验证是指将百度给你的xml文件放到source目录下(即source/BingSiteAuth.xml)

    2. HTML Meta 标记验证

      • html标签验证则是在head标签里添加百度所指定的代码
        <meta> 标记复制并粘贴到你网站的主页中。该标记应位于第一个 <body> 部分前面的 <head> 部分中。
        <meta name="msvalidate.01" content="xxxxxx" />

      要保持已验证状态,请勿移除 meta 标记,即使验证成功后也是如此.

    3. CNAME验证 (推荐)
      手动将 CNAME 记录添加到域名服务器中
      添加名为 xxx 且值为 verify.bing.com 的 CNAME (别名)记录。

  • 以上三种验证方式, 任选一种, 进行验证

1.4. 优化结构

seo搜索引擎优化认为,网站的最佳结构是用户从首页点击三次就可以到达任何一个页面,但是我们使用hexo编译的站点打开文章的url是:sitename/year/mounth/day/title四层的结构,这样的url结构很不利于seo,爬虫就会经常爬不到我们的文章,于是,我们可以将url直接改成sitename/title的形式,并且title最好是用英文,在根目录的配置文件下修改permalink如下:

url: http://你的网站
root: /
permalink: :title.html
permalink_defaults:
pretty_urls:
  trailing_index: false # Set to false to remove trailing 'index.html' from permalinks
  trailing_html: true # Set to false to remove trailing '.html' from permalinks

这里建议添加html后缀, 这样搜索引擎从url后缀也能判断是html页面
这里建议将trailing_index 设置为false 这样生成的目录结构又会缩短一层变成'http://你的网站/category/title.html'
如果不设置为false, 则页面的永久连接为'http://你的网站/category/title/index.html'

1.5. 添加sitemap站点地图

sitemap站点地图可以让搜索引擎更便捷的抓取你的网站,一般为XML文件。

hexo有两个插件可以自动生成sitemap:

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

安装完成后,进入hexo根目录的_config.yml配置文件,添加以下代码:

1.6. 自动生成sitemap

# common sitemap for all the search engines
sitemap:
  path: sitemap.xml
# for baidy
baidusitemap:
  path: baidusitemap.xml

每次运行hexo g ,则会在缓存文件夹下生成sitemap.xml baidusitemap.xml则为成功。

生成了sitemap后,则需要提交sitemap给搜索引擎:

百度则是进入搜索资源平台的站点管理,点击sitemap,添加baidusitemap.xml的路径
谷歌则是进入Google Search Console,进入站点地图,添加sitemap.xml的路径
其中 sitemap.xml 文件是搜索引擎通用的 sitemap 文件

1.7. 关键词与描述的优化

keywords和description是告诉搜索引擎你这个网页的关键词和你这个网页是干什么的,不能乱写。

1.8. 代码压缩

网页的代码存在大量空白符,压缩代码可以提高网站被访问速度。

安装以上插件:


npm install hexo-neat --save

配置插件

修改根目录下的配置文件_config.yml添加以下代码:


neat_enable: true
neat_html:
  enable: true
  exclude:
neat_css:
  enable: true
  exclude:
    - '**/*.min.css'
neat_js:
  enable: true
  mangle: true
  output:
  compress:
  exclude:
    - '**/*.min.js'
    - '**/index.js'

1.9. 添加robots.txt

在网站优化过程中,有些时候,网站中有重要及私密的内容,站长并不希望某些页面被蜘蛛抓取,比如后台的数据,测试阶段的网站,还有一种很常见的情况,搜索引擎抓取的大量没有意义的页面。

robots.txt是一个纯文本文件,用于声明该网站中不想被蜘蛛访问的部分,或指定蜘蛛抓取的部分,当蜘蛛访问一个站点时,它会首先检查该站点是否存在,robots.txt,如果找到,蜘蛛就会按照该文件中的内容来确定抓取的范围,如果该文件不存在,那么蜘蛛就会沿着链接直接抓取。即,只有在需要禁止抓取某些内容是,写robots.txt才有意义.

robots配置方法如下:

  • 在 .../source 路径下创建robots.txt文件,里面输入你的robots配置,如果不懂,可以百度robots的语法,修改后保存即可提交。

例如:

# welcome to : eagle's blog
User-agent: *
Allow: /
Allow: /archives/
Allow: /categories/
Allow: /about/

Disallow: /js/
Disallow: /css/
Disallow: /fonts/
Disallow: /vendors/
Disallow: /lib/
Sitemap: https://your_site_address/sitemap.xml

注意Disallow与Allow行的顺序是有意义的

举例说明:
允许爬虫蜘蛛访问 /admin/ 目录下的seo文件夹  
  User-agent: *     
  Allow: /admin/seo/
  Disallow: /admin/

如果Allow 和 Disallow 的顺序调换一下:

  User-agent: *     
  Disallow: /admin/
  Allow: /admin/seo/
  爬虫蜘蛛就无法访问到 /admin/ 目录下的 seo 文件夹,因为第一个 Disallow: /admin/ 已匹配成功。

在百度站长平台监测并更新Robots
提示检测到您更新了Robots文件即成功。

2. 开启百度推送

每次更新博客时,主动向百度提交链接,有利于SEO.

  • 安装 hexo-baidu-url-submit
  npm install hexo-baidu-url-submit --save
  # 使用淘宝cnpm可以加快速度
  cnpm install hexo-baidu-url-submit --save
  • 修改根目录下的_config配置

    设置百度主动推送

      baidu_url_submit:
        count: 200  # 比如200,代表提交最新的200个链接
        host: www.pengtech.net # 在百度站长平台中注册的域名
        token: your_token # 百度站长密钥
        path: baidu_urls.txt # 文本文档的地址, 新链接会保存在此文本文档里,这个默认
    
  • 修改根目录下的_config配置 Deploy

  • 检验是否成功
    当出现类似以下信息,表示配置成功.

    INFO  Deploying: baidu_url_submitter
    INFO  Submitting urls
    https://pengtech.net/20211028/linux/create_rpm_repository_on_linux/
    https://pengtech.net/20211028/linux/how_to_create_rpm_package/
    {"remain":2978,"success":22}
    INFO  Deploy done: baidu_url_submitter
    

3. 谷歌收录站点

进行这个之前,首先友情提示下大佬们,最好开个vpn用,因为很多涉及谷歌服务,我这里用的是789vpn,网上有很多,可以去找,lantern也能用

打开 Google Search Console,添加博客地址。并进行相关验证,根据提示即可完成认证,认证成功如下:

认证方法其实这里方法和百度的类似,也有三种,我用的是直接下载文件,放到我的站点source目录下,然后我在git命令面板里输入以下指令(下面将这一套指令简称为更新并同步文件指令)

hexo clean && hexo g && hexo d

之后,我去google认证,发现并不能验证成功,后来去站点配置_config.yml下添加了一句代码,和百度的类似

google-site-verification: true

4. 首页标题优化

SEO最重要的是你的标题,一般搜索都是搜索你的标题。
更改index.swig文件(your-hexo-site\themes\next\layout\layout.njk);

<title>{% block title %} {% endblock %}</title>

修改为:

<title>{% block title %} - {{ theme.description }} {% endblock %}</title>

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

5. 补充: 让bing, google, baidu快速收录网站

对于Hexo博客,我们需要快速将内容主动提交给搜索引擎,其他人仅搜索文章的关键字时才能看到我们的文章,从而提高了曝光率。
这里需要安装插件 并修改hexo配置 hexo-submit-urls-to-search-engine
安装:

npm install --save hexo-submit-urls-to-search-engine

配置:
需要修改hexo的配置文件_config.yml

hexo_submit_urls_to_search_engine:
  submit_condition: count # count/period The submitted condition, only supports count now
  count: 20 ## Submit the latest 10 links
  period: 900 #A period of time, in seconds, the update time of the article is away from the running time of the plugin. Within this period of time, the link will be submitted
  google: 0 #  1/0->on/off   Submit to Google
  bing: 1 #Submit to bing
  baidu: 1 #Submit to baidu
  txt_path: submit_urls.txt ## The address of the text document, the link to be pushed will be saved in this text document
  baidu_host: https://www.pengtech.net ## Domain name registered in Baidu webmaster platform
  baidu_token: xxxxx ## Please note that this is your secret key, so do not publish the website source code in the public repository!
  bing_host: https://www.pengtech.net ## Domain name registered in Bing webmaster platform
  bing_token: xxxxx ## Please note that this is your secret key, so do not publish the website source code in the public repository!
  google_host: https://www.pengtech.net ## Domain name registered in Google webmaster platform
  google_key_file: Project.json #Store the json file of the google key in the root directory of the website (same location as the hexo config.yml file), please do not publish the source code of the website in the public warehouse!

5.1. 如何获得bing_token

登录bing webmasters, 点击设置-> API访问 管理凭据 -> API密钥 管理密钥 -> 即可看到api密钥.

5.2. 修改deploy

deploy:
- type: cjh_google_url_submitter
- type: cjh_bing_url_submitter
- type: cjh_baidu_url_submitter

发布

hexo clean && hexo generate && hexo deploy

此时可以看到日志中已经可以提交连接到百度和bing

6. 参考文档

hexo博客的高级SEO优化

robots.txt的语法和写法详解

Hexo站点建设之——给自己的网站加上robots.txt

hexo搭建个人博客之seo优化

hexo博客快速被谷歌,必应,百度收录

HEXO SEO 高级优化

Hexo seo优化之关键字和路径优化

Hexo - Meta - SEO