Github pages 404页面自定义

Github help有专门的资料。

github-help-404

需要注意的是404页面自定义,必须有顶级域名,并且该域名已经解析到了Github pages,例如username.github.io。

具体步骤是在Github网站仓库中新建一个404.html,或者404.md文件。然后对该404文件进行YAML front matter,YAML前标记也就是在文件的开头按照固定格式为预定义或自定义变量赋值。

$ cd wisetc.github.io
$ curl baidu.com| sed -e '1i\---' -e $'1i\permalink: /404.html' -e '1i\---' > 404.html
$ sed -i 's/baidu/uqugu/g; 5s/0/5/g' 404.html
$ cat 404.html
$ sed -i '5a\没有找到您要访问的页面,请通过www网址访问此页,5秒钟后将跳转回首页。' 404.html
$ 
$ git add 404.html
$ git commit -m '404 custom'
$ git push

修改后的404.html文件内容大概是

---
permalink: /404.html
---
<html>
<meta http-equiv="refresh" content="5;url=http://www.uqugu.com/">
没有找到您要访问的页面,请通过www网址访问此页,5秒钟后将跳转回首页。
</html>

提交代码后,访问404页面生效了

404-demo

对于不带www的主站,跳转到带www的站点,可以利用js实现,404.html文件

---
permalink: /404.html
---
<html>
<script>
  var url=window.location.href;
  window.location.href = url.replace('://', '://www.');
</script>
</html>

原文链接

关于本文如您有任何想法和意见,欢迎与我们联系,邮箱地址zhi@uqugu.com
您对本文有什么看法,喜欢或者不喜欢都可以发表意见。