【问题标题】:How to redirect automatically after short delay on Error 404 page?如何在错误 404 页面上短暂延迟后自动重定向?
【发布时间】:2013-08-04 11:15:50
【问题描述】:

我已经设置了一个自定义 404 页面here

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

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

【问题讨论】:

标签: html css .htaccess redirect


【解决方案1】:

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

<meta http-equiv="refresh" content="3;URL='http://www.example.com/404.html'" />

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

【讨论】:

【解决方案2】:

只需在您的 404 错误页面中添加一点 PHP。

我假设你的 htaccess 文件中有这个,ErrorDocument 404 /error/404/

所以在/error/404 上,您可以在任何地方执行以下操作。

<?php
    header("refresh:5; url=/wherever.html"); 
?>

请务必将5 更改为您的秒数,将wherever.html 更改为您的拍子。

【讨论】:

  • 是的,我的.htaccess 中有这个ErrorDocument 404 /404.html。我对 PHP 的了解为零。我应该把它放在哪里?此外,元刷新标签对我有用。与另一个相比,我对 php 有什么好处吗?
  • @SamDG 使用一些 php 并学习其中一些是个好主意。基本上,无论您将其放在文件中的何处,它都会运行。 php > html 或 html > php 无论哪种方式它仍然运行。
【解决方案3】:

您应该使用客户端重定向方法来应用适当的延迟。如果您想支持禁用 JavaScript 的浏览器,只需将 META refresh 粘贴到 noscript 元素中即可。最好将其放在 noscript 元素中,因为搜索引擎会对通常的 META refresh 链接进行惩罚。

如果重定向是永久的,我建议你也使用canonical标签来保留丢失页面的链接汁。

这个 JavaScript redirect 生成器 是一个不错的选择。从那里粘贴的代码。它还有一个 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 -->

【讨论】:

  • 感谢您的详细回答:)
猜你喜欢
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 2015-07-26
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
相关资源
最近更新 更多