如何实现GitHub个人空间路径无tree/main/访问指定repo内容?
解决GitHub Pages路径跳转问题的方案
方案1:Jekyll内置重定向(最省心)
- 编辑你
username/username仓库根目录的_config.yml,添加:
redirect_from: - /repo
- 在根目录新建
repo.md文件,内容:
--- redirect_to: https://github.com/username/repo ---
提交后等GitHub Pages重新构建,访问https://github.com/username/username/repo会直接跳转到正确仓库,路径不会带tree/main/。
方案2:自定义404页面兜底
如果Jekyll重定向没生效,用404页面做跳转:
在仓库根目录创建404.html,写入:
<script> if (window.location.pathname === '/repo') { window.location.replace('https://github.com/username/repo'); } </script>
当访问/repo触发404时,JS会自动跳转到目标地址。
方案3:分支绑定GitHub Pages
- 在
username/username仓库新建一个名为repo的分支 - 在该分支根目录创建
index.html,内容:
<meta http-equiv="refresh" content="0; url=https://github.com/username/repo">
- 进入仓库「Settings」→「Pages」,把源分支改成
repo并保存。
这样访问指定路径时,会加载这个分支的页面并自动跳转。
注意:所有修改提交后,GitHub Pages可能需要1-5分钟完成部署,耐心等一下再测试。
内容的提问来源于stack exchange,提问作者kwalker
相关产品推荐
相关产品推荐

