0%

GitHub Pages解决根域名跳转问题及多域名问题

我的首页是建立在github pages上的,并且使用了自定义域名。www.tangbao.me被CNAME到了tangbao.github.io上,github pages的证书也被issue到了www.tangbao.me,但是我却把tangbao.me CNAME到了www.tangbao.me上。这会导致使用tangbao.me访问我的首页的时候,浏览器会报证书错误。

因为github pages的配置里只支持一个custom domain (也就是CNAME文件里只能有一个域名),所以不能把tangbao.mewww.tangbao.me同时添加到首页repo的CNAME中去。同样的问题也会出现在github pages需要两个及以上不同域的域名时。

查了一些文档和咨询了大佬们之后,我终于想起来了301跳转这件事情,于是解决方案就非常显而易见了,只要把http://tangbao.me/*或者https://tangbao.me/*的请求强制301转发到https://www.tangbao.me/$1就可以了($1表示前面的*所代表的参数)。以Cloudflare为例,你可以在Page Rule设置里设置301跳转。设置完成之后的rule description如下:

1
2
tangbao.me/*
Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://www.tangbao.me/$1)

参考资料:

Cloudflare - Understanding and Configuring Cloudflare Page Rules (Page Rules Tutorial)

GiHub - About custom domains and GitHub Pages