大家知道hexo默认的链接是http://xxx.yy.com/2013/07/14/hello-world这种类型的,这源于站点目录下的配置_config.yml里的配置:permalink: :year/:month/:day/:title/.这种默认配置的缺点就是一般文件名是中文,导致url链接里有中文出现,这会造成很多问题,如使用gitment,也不利于seo。另外就是年月日都会有分隔符。本文介绍经过作者研究总结出的最终解决方案。
在md文件的Front-matter区域,增加一个urlname属性,用来放文章的英文名字。如下图所示:
title: hexo链接持久化终极解决之道 urlname: hexo_permalink date: 2017-08-31 23:53:24 comments: true categories: - 综合 tags: - hexo然后修改 _config.yml里的配置:
permalink: posts/:category/:year:month:day-:urlname.html permalink_defaults: urlname: index后缀增加.html给人以静态页面感觉,利于seo.这里面:urlname表示引用的这个变量。前面是分类/年月日。另外增加一个urlname这个属性的默认值,当md文档里不填这个属性,就用这个值。这里设的是index,也就是说如果不填这个属性,其链接为:http://xxx.com/posts/uncategorized/20170828-index.html。当然最好写上这个属性,偶尔某天不写这个属性也没关系。
另外还要解决一个问题,就是:category分类有可能为中文。为此在站点下配置文件里设置:
default_category: uncategorized category_map: 综合: common这样在Front-matter里,分类该写中文写中文,最终的链接会翻译成英文。
无意中看到http://muyunyun.cn/posts/f55182c5/#more,最终决定采用这种方案使用hexo-abbrlink这个插件,猜测是根据时间点算出的最终链接,后期不管怎么修改永久链接都不会变.一来自己不用再增加什么属性了,也不用考虑分类中文化的问题。二来后面的层级更短,更利于SEO。(一般SEO只爬三层)
npm install hexo-abbrlink --save站点配置文件里:
permalink: post/:abbrlink.html abbrlink: alg: crc32 # 算法:crc16(default) and crc32 rep: hex # 进制:dec(default) and hex另外可以修改scaffolds里的模版文件,修改post.md为:
--- title: {{ title }} date: {{ date }} comments: true categories: tags: ---