文章链接获取错误的问题
检查清单
问题描述
自hexo-theme-stellar 1.33.0版本起,发现在hexo的permalink配置中,出现以.html结尾的配置时,首页无法build出
正确的链接,同时,sitemap等插件也无法正确拿到文章链接。怀疑与1.33.0中新增加的pretty_url特性有关。
附上依赖版本
{
"hexo": "^7.3.0",
"hexo-generator-archive": "^2.0.0",
"hexo-generator-category": "^2.0.0",
"hexo-generator-index": "^4.0.0",
"hexo-generator-tag": "^2.0.0",
"hexo-related-popular-posts": "^5.0.1",
"hexo-renderer-ejs": "^2.0.0",
"hexo-renderer-marked": "^7.0.0",
"hexo-renderer-stylus": "^3.0.1",
"hexo-server": "^3.0.0",
"hexo-theme-landscape": "^1.0.0",
"hexo-theme-stellar": "1.33.1"
}
不用怀疑( 这是作者的答复:
为了处理 canonical 问题,确保不会存在一个地址多种 canonical,所以只能强制统一内部链接格式。
可以在链接末尾加上一个不影响链接访问但不会被 pretty_url “优化”的字符,比如 #。
[!note] https://www.travellings.cn/go-robots.html#
不用怀疑( 这是作者的答复:
为了处理 canonical 问题,确保不会存在一个地址多种 canonical,所以只能强制统一内部链接格式。
可以在链接末尾加上一个不影响链接访问但不会被 pretty_url “优化”的字符,比如
#。Note
https://www.travellings.cn/go-robots.html#
有相关的issue或discuss吗?我去查看下~
有相关的issue或discuss吗?我去查看下~
讨论串在我的博客上 : ) 因为是我遇到这个问题然后自己鼓捣了一个临时解决方法,被 XAOXUU 评论了 😸
链接:https://github.com/rt265/Blog-Comments/discussions/1#discussioncomment-13743431
同步了下配置更新,发现了类似的问题,文章页面全部 404。 不过我这里主要是和 nginx 与永久链接设置有关,这里发下给遇到相同问题的朋友一个参考思路。
我最初使用的 permalink 配置:
permalink: posts/:abbrlink.html
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
这样最终会生成这种目录结构:
public/posts
|-- aaa.html
|-- bbb.html
`--ccc.html
但是新版主题增加了 override_pretty_urls: true,实际上就是将 trailing_index 和 trailing_html 都设置成了 false,修改后访问文章会生成这种 URL:
demo.com/posts/aaa <-- hexo server,可以正常访问
demo.com/posts/aaa/ <-- nginx,404
URL 最后的斜线是 nginx 自动添加的,这个斜线会导致 nginx 的 try_files 匹配规则不太好写,如果全部 rewrite 去除最后的斜线,那么 about/,categories/ 这种页面就会受影响。
最后的解决方法是,既然 nginx 默认生成“目录”形式的 url(带最后的斜线),那就把 permalink 搞成目录:
permalink: posts/:abbrlink/index.html
生成目录结构:
public/posts
|-- aaa
| `-- index.html
|-- bbb
| `-- index.html
`-- ccc
`-- index.html
最终 override_pretty_urls: true 设置会把 index.html 隐藏掉,这样无需修改 nginx 配置即可正常访问 demo.com/posts/aaa/。
当然 nginx 配置中的 try_files 需要包含 $uri/index.html
permalink: posts/:abbrlink/index.html
这里设置成 permalink: posts/:abbrlink/也是一样的效果。
我这属于是个人喜好了,习惯文章链接使用.html结尾
没想到这个改动有这么多bug,不行考虑回退代码算了。