静态博客网站重定向

非法路径重定向到404页面,在404页面设置js脚本3秒后跳转到首页,对搜索引擎友好

1 自定义404页面

在根目录下新建404.md文件,添加跳转首页的js代码

<script>
setTimeout(function() {
window.location.href = "/";
}, 3000); // 5秒后跳转
</script>
<center>页面不存在,即将跳转至首页...</center>

2 重定向配置

本博客部署在cloudflare pages,支持在根目录下添加_redirects文件来配置重定向,在source根目录下添加_redirects文件,但是hexo会忽视下划线开头的文件,导致hexo构建的时候,没有将_redirects移动到public路径下,因此在source目录下添加没有下划线的redirects文件,然后用在cloudflare pages的构建命令中进行重命名

^/(?!(about|categories|demo|images|library|nagging|repost|tags|posts)/).*$  /404.html  302

以上配置是指除了about|categories|demo|images|library|nagging|repost|tags|posts开头的,通通重定向到404页面,在cloudflare pages构建命令中添加:

mv public/redirects public/_redirects

更新日志

  • 20250521: 初稿