如何在错误404页面上短暂延迟后自动重定向?

Sam*_* DG 2 html css .htaccess redirect

在这里设置了自定义404页面

我编辑了.htaccess文件以使404页面正常工作.

我想要的是在404页面上的短暂延迟后,当他点击一个不存在的链接时,自动将访问者重定向到主页.

Pra*_*inS 14

在404页面中使用元刷新标记

<meta http-equiv="refresh" content="3;URL='http://www.example.com/404.html'" />
Run Code Online (Sandbox Code Playgroud)

它将根据内容值(以秒为单位)和您要重定向的URL重定向


Pat*_*lán 5

您应该使用客户端重定向方法来应用适当的延迟。如果要支持禁用JavaScript的浏览器,只需META refresh将a 粘贴到一个noscript元素中。最好将其放置在noscript元素中,因为搜索引擎会对常规META refresh链接造成不利影响。

如果重定向是永久的,我建议您也使用该canonical标记,以保持缺少页面的链接状态。

JavaScript重定向生成器是一个很好的方法。从那里粘贴的代码。它还具有IE 8和更低的修复程序以传递HTTP引用,这对于流量分析很有用。

<!-- Pleace this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://example.com/"/>
<noscript>
    <meta http-equiv="refresh" content="5;URL=https://example.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://example.com/";
    var delay = "5000";
    window.onload = function ()
    {
        setTimeout(GoToURL, delay);
    }
    function GoToURL()
    {
        if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
        {
            var referLink = document.createElement("a");
            referLink.href = url;
            document.body.appendChild(referLink);
            referLink.click();
        }
        else { window.location.replace(url); } // All other browsers
    }
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
Run Code Online (Sandbox Code Playgroud)