Github Pages自定义子域名场景下Jekyll baseurl配置被忽略如何解决
问题解决方案
Github Pages 绑定自定义域名的仓库默认会将所有内容发布到域名根路径,自带的Jekyll构建流程会强制覆盖_config.yml中的baseurl配置,这是平台默认行为,可通过以下任意一种方案解决:
方案一:调整目录结构(最稳定,优先推荐)
- 在绑定
sub.domain.com的Github仓库根目录新建subdirectory文件夹 - 把仓库内所有Jekyll站点源码(根目录的
CNAME文件除外)全部移入该文件夹 - 删除
_config.yml中之前添加的baseurl: /subdirectory配置 - 推送改动到Github等待部署完成后,站点内容就会自动在
sub.domain.com/subdirectory路径下正常加载,根路径可根据需要单独添加跳转页面
方案二:自定义构建流程覆盖默认配置
- 保留
_config.yml中的baseurl: /subdirectory配置 - 在仓库根目录新建
.github/workflows/jekyll-deploy.yml文件,使用自定义Action构建站点,规避默认构建的baseurl覆盖逻辑,参考配置如下:
name: Jekyll Deploy on: push: branches: [ main ] # 替换为你实际的源码分支名 jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Build Jekyll site uses: actions/jekyll-build-pages@v1 with: baseurl: "/subdirectory" source: ./ destination: ./_site - name: Upload build artifact uses: actions/upload-pages-artifact@v3 deploy: needs: build permissions: pages: write id-token: write environment: name: github-pages url: ${{ steps.deploy.outputs.page_url }} runs-on: ubuntu-latest steps: - name: Deploy to Pages id: deploy uses: actions/deploy-pages@v4
- 进入仓库
Settings > Pages页面,将构建来源从Deploy from a branch改为GitHub Actions即可
方案三:根路径跳转(临时兼容方案)
- 保留原有
baseurl配置不变 - 在仓库根目录新建
index.html,添加自动跳转逻辑,访问sub.domain.com时自动跳转到目标路径,参考代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="refresh" content="0; url=/subdirectory/"> </head> <body> <p>如果没有自动跳转,请<a href="/subdirectory/">点击这里</a></p> </body> </html>
内容的提问来源于stack exchange,提问作者Ephebopus
相关产品推荐
相关产品推荐

