You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将GitHub Pages默认首页设为loading.html而非index.html?

GitHub Pages 设置自定义默认首页(loading.html)的解决方案

核心结论

GitHub Pages 严格要求默认首页为根目录下的index.html,没有官方提供的配置项或设置可以直接修改这一默认行为。你尝试的.htaccess和_config.yml无效,原因如下:

  • .htaccess是Apache服务器的配置文件,GitHub Pages不支持自定义这类服务器级配置;
  • _config.yml是Jekyll的配置文件,Jekyll同样默认以index.html作为站点首页,没有可修改默认首页文件名的配置项。

可行替代方案

方案1:将loading.html设为根目录的index.html

这是最直接且稳妥的方案,具体操作:

  • 直接将src/loading-page/loading.html复制或重命名到dist根目录,命名为index.html;
  • 如果是通过构建工具生成文件,可以在构建脚本中添加自动复制/重命名的步骤,比如用npm脚本:
    "scripts": {
      "build": "your-build-command && cp src/loading-page/loading.html dist/index.html"
    }
    

这样用户访问你的站点时,会直接加载这个index.html(也就是原来的loading.html),完全符合需求。

方案2:用根目录index.html做自动跳转

如果不想修改原文件的名称或位置,可以在dist根目录新建一个index.html,通过代码自动跳转到loading.html:

  • Meta标签跳转(无JS依赖,跳转速度快):
    <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="refresh" content="0; url='src/loading-page/loading.html'">
    </head>
    </html>
    
  • JavaScript跳转(灵活性更高,可添加跳转前逻辑):
    <!DOCTYPE html>
    <html>
    <head>
      <script>
        window.addEventListener('load', () => {
          window.location.href = 'src/loading-page/loading.html';
        });
      </script>
    </head>
    </html>
    

⚠️ 注意:这种跳转方式可能会出现短暂的空白页面,且对SEO有一定影响,仅推荐作为临时解决方案。

总结

没有官方途径能让GitHub Pages直接将loading.html设为默认首页,最可靠的方式是采用方案1,将着陆页文件设为根目录的index.html。

内容的提问来源于stack exchange,提问作者Azuronate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:40:42