网站跳转出现404怎么办?肯定是想办法把蜘蛛圈住。下面,一起来看教程。
访问404页面时,跳转到某篇文章,蜘蛛遇到404页面时,随机跳转到某一篇正常的文章,这样就不会浪费任何一个蜘蛛。
教程开始
好的,开整。在根目录新建一个 404.php 文件,代码如下。下面我给两套代码,一套是直接跳转,一套是有预加载动画计时跳转。
代码一:直接跳转
<?php error_reporting(0); header('Content-type:text/html;charset=utf-8'); require_once (dirname(__FILE__).'/include/common.inc.php'); $row = $dsql->GetOne("SELECT id,title FROM `#@__archives` WHERE id>=(SELECT FLOOR( MAX(id) * RAND()) FROM `#@__archives`) ORDER BY RAND() LIMIT 0,1 "); $result = GetOneArchive($row['id']); if ($cfg_multi_site == 'Y'){ $arcurl = $result['arcurl']; }else{ $arcurl = $cfg_basehost.$result['arcurl']; } $arcurl = str_replace($cfg_basehost.$cfg_basehost,$cfg_basehost, $arcurl); header('Location: '.$arcurl.'');//直接跳转到文章页 ?>
这是直接跳转的代码。
代码二:预加载动画
<?php error_reporting(0); header('Content-type:text/html;charset=utf-8'); require_once (dirname(__FILE__).'/include/common.inc.php'); $row = $dsql->GetOne("SELECT id,title FROM `#@__archives` WHERE id>=(SELECT FLOOR( MAX(id) * RAND()) FROM `#@__archives`) ORDER BY RAND() LIMIT 0,1 "); $result = GetOneArchive($row['id']); if ($cfg_multi_site == 'Y'){ $arcurl = $result['arcurl']; }else{ $arcurl = $cfg_basehost.$result['arcurl']; } $arcurl = str_replace($cfg_basehost.$cfg_basehost,$cfg_basehost, $arcurl); header('Refresh:3;url='.$arcurl.'');//3秒后跳转到文章页 ?> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <title>加载中,请稍候...</title> <style type="text/css"> body{background:#fff}.dqhk{position:fixed;animation:ball-s .5s 0s ease-out;top:-60px;left:0;bottom:0;right:0;display:flex;align-items:center;justify-content:center}.qjdh{transform:scale(1) translateY(-30px)}.qjdh>div:nth-child(2){-webkit-animation-delay:-.4s;animation-delay:-.4s}.qjdh>div:nth-child(3){-webkit-animation-delay:-.2s;animation-delay:-.2s}.qjdh>div{position:absolute;top:0;left:-30px;margin:2px;margin:0;width:15px;width:60px;height:15px;height:60px;border-radius:100%;background-color:#ff3cb2;opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation:ball-scale-multiple 1s .5s linear infinite;animation:ball-scale-multiple 1s .5s linear infinite}.lohg{position:fixed;top:60px;left:0;bottom:0;color: #f156b4;animation:ball-s2 .8s cubic-bezier(0.36, 0.29, 0.62, 1.36);right:0;display:flex;align-items:center;justify-content:center;}@-webkit-keyframes ball-scale-multiple{0%{opacity:0;-webkit-transform:scale(0);transform:scale(0)}5%{opacity:1}to{-webkit-transform:scale(1);transform:scale(1)}}@keyframes ball-scale-multiple{0%,to{opacity:0}0%{-webkit-transform:scale(0);transform:scale(0)}5%{opacity:1}to{opacity:0;-webkit-transform:scale(1);transform:scale(1)}}@keyframes ball-s{0%,to{opacity:0;transform:scale(0)}to{opacity:1;transform:scale(1)}}@keyframes ball-s2{0%{opacity:0}30%{opacity:0}to{opacity:1}} </style> <script type="text/javascript"> var num = 1; function Trect() { num++; document.getElementById("num").innerHTML = num; } setInterval("Trect()", 1000); </script> </head> <body> <div class="dqhk"> <div class="qjdh"> <div></div> <div></div> <div></div> </div> </div> <div class="lohg">加载中,请稍候...<div id="num">1</div>秒</div> </body> </html>
这是加了预加载动画的代码。
设置404页面
在宝塔面板,网站管理,配置文件中
error_page 404 /404.html;
改为
error_page 404 /404.php;
完成了。当然了,你也可以不用改这个,那就是改个伪静态规则。
Apache伪静态规则
RewriteEngine on RewriteRule ^404.html$ 404.php
如果已经有 RewriteEngine on 的,就不需要 RewriteEngine on 了。
Nginx伪静态规则
rewrite ^/404.html$ /404.php;
完成了。
+63 63 个回复 | 最后更新于 2024-12-11
登录后方可回帖