如何在错误404页面上短暂延迟后自动重定向? [英] How to redirect automatically after short delay on Error 404 page?

查看:86
本文介绍了如何在错误404页面上短暂延迟后自动重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我编辑了404页面的.htaccess文件。

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

解决方案

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



如果重定向是永久性的,我建议您也使用规范标签来保留缺少页面的链接。



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

 <! -  - 在打开头标以使其正常工作后,立即将这一片段撕下来 - > 

<! - 此代码在GNU GPL v3下授权 - >
<! - 您可以自由复制,分发和使用此代码,但严禁删除作者信用 - >
<! - 由http://insider.zone/tools/client-side-url-redirect-generator/生成 - >

<! - 重新开始起步 - >
< link rel =canonicalhref =https://example.com//>
< noscript>
< meta http-equiv =refreshcontent =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);
}
函数GoToURL()
{
if(typeof IE_fix!=undefined)// IE8及更低版本传递http referer
{
var referLink = document.createElement(a);
referLink.href = url;
document.body.appendChild(referLink);
referLink.click();
}
else {window.location.replace(url); } //所有其​​他浏览器
}
< / script>
<! - Credit转到http://insider.zone/ - >
<! - 重定向 - >>


I have setup a custom 404 page here

I have edited the .htaccess file for the 404 page to work.

What I want is to redirect a visitor to the home page automatically after a short delay on the 404 page, when he clicks on a non-existing link.

解决方案

You should use a client side redirection method to apply the proper delay. If you want to support browsers with disabled JavaScript, just paste a META refresh into a noscript element. It is better to place it inside a noscript element because search engines give a penalty for usual META refresh links.

If the redirect is permanent, I suggest you to use the canonical tag too to keep the link-juice of missing pages.

This JavaScript redirect generator is a good way to go. The code pasted from there. It also has an IE 8 and lower fix to pass the HTTP referer, which is useful for traffic analysis.

<!-- 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 -->

这篇关于如何在错误404页面上短暂延迟后自动重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆