有没有一种方法可以使用`<a href="/">` with github pages?

时间:2019-05-14 02:09:55

标签: html url github uri github-pages

How can I make <a href="/"> redirect to https://[username].github.io/[repo] instead of https://[username].github.io/ with github pages?

When serving my multi-page html website, I get a 404 - Not Found every time I click on an <a href="/"> link on my pages. Instead of redirecting to https://mepley.github.io/github-pages-redirect-to-index/ where index.html is found, I am instead redirected to https://mepley.github.io/ and thus I get the 404 - Not Found message.

Here is a link to a github pages site to demonstrate the issue (served from master branch): https://mepley.github.io/github-pages-redirect-to-index/

我的最终目标是仅使用另一个HTML文档中的<a href="/">并重定向回index.html。 重要提示:我不愿意适应其他任何策略,例如将<a href="/index.html"><a href="./"><a href="../">作为相对参考。

我之所以坚持只使用"/"是因为我正在创建一个样板Webpack配置,用于首次构建静态网站,并开始使用编码人员将简单的网站部署到github页面,宁愿不必向刚刚学习并且一开始就很困惑的人解释这种特质。

我尝试使用<base href="https://mepley.github.io/github-pages-redirect-to-index">标签,但这不能解决问题。我也尝试了其他<base>标签,但没有成功(但也许我做错了吗?)。而且,正如我在下面的评论中所解释的,我也知道当前页面(即<a href="../">)的相对引用实际上将重定向到正确的首页,但是它仍然回答我的问题,不幸的是。

这里是演示此问题的唯一两个文件:

index.html

<!DOCTYPE html>
<html>
  <body>
    This is index.html
    <a href="./another-page.html">goto another-page.html</a>
  </body>
</html>

another-page.html

<!DOCTYPE html>
<html>
  <body>
    This is another-page.html
    <a href="/">go back to index.html</a>
  </body>
</html>

预期结果是,我单击<a href="/">中的another-page.html链接,它将我重定向回https://mepley.github.io/github-pages-redirect-to-index/,并找到index.html而不是https://mepley.github.io/当前正在发生404错误。

在此问题上的任何帮助将不胜感激!

此外,这是该站点的回购链接:https://github.com/mepley/github-pages-redirect-to-index

1 个答案:

答案 0 :(得分:2)

最好的解决方案是您不想做的事情: <a href="/index.html"/>。 请说明为什么您不想这样做。只有10个额外的字符。