Jekyll + GitHub 页面站点中的尾部斜杠导致 404

Trailing slashes in Jekyll + GitHub Pages site cause 404

我希望在我的网站上解析以下所有 URL,该网站使用 Jekyll 构建并托管在 GitHub 页面上:

在本地它们都可以正常工作,但现在在实时站点上,第一个和第三个选项可以解析,但中间带有尾部斜杠的选项会导致 404 错误。

我目前没有使用固定链接。当我 dopermalink: /about/ 添加到我页面的前面时,尾部斜杠问题得到解决,但随后 about.html 执行 404。我想这比当前行为,但我更希望所有三个选项都单独工作或重定向到一个单独工作。

如果相关,我在布局模板的 <head> 中设置了规范引用,如下所示:

<link rel="canonical" href="{{ site.url }}{{ page.url | replace:'index.html',''}}">

这是我的 _site 的文件树:

因此,如果您在本地构建站点时查看生成的 _site 文件夹,您会看到应该有以下内容:

_site
  |--about
  |   |--index.html
  |   |
...

使用永久链接 /about/ 和最后的 / 意味着 Jekyll 将创建 about 文件夹,然后在里面创建 index.html 页面。由于历史优先级,它被称为 index.html。浏览器默认在没有要检索的特定文件时查找此页面。

考虑到这一点,这三个选项中的每一个都会发生以下情况:

  1. /about:浏览器足够聪明,知道插入尾随 /,因此会查看 /about/ 文件夹。您没有指定要查找的特定文件,因此它默认查找 index.html。它找到 index.html 并呈现它。
  2. /about/:同上。它在 /about/ 文件夹中查找。由于未指定特定文件,因此它会查找 index.html。它找到 index.html 并呈现它。
  3. /about.html:浏览器正在专门查找位于根文件夹中名为 about.html 的文件。 /about/index.html 在那里,但这不是浏览器正在寻找的。 about.html 不存在,这就是它抛出 404 的原因。

所以,没有错误。这就是当您在 url.

中给它尾随 / 时浏览器的行为方式

根据 GitHub 的支持,这是 GitHub 页中的预期行为:

Hello Michael,

Thanks for contacting GitHub Support with your questions about GitHub Pages.

There's is currently no way to change our trailing slash behavior at this time, though I do understand that a situation like this can be quite frustrating.

I'll share your use case with the team for consideration in future improvements. I can't say if or when a change will happen, but your feedback is in the right hands.

Thanks,

Steve @slgraff GitHub Support

已解决:运行 遇到同样的问题并通过更新 _config.yml

中的永久链接设置修复了该问题

在此处添加尾部斜杠。缺少尾部斜杠的 URL 将重定向到 /:name/

https://jekyllrb.com/docs/permalinks/

collections:
  my_collection:
    output: true
    permalink: /:collection/:name/