2

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.

Sam DG
  • 79
  • 1
  • 2
  • 11
  • 1
    possible duplicate of [Redirect website after certain amount of time](http://stackoverflow.com/questions/3292038/redirect-website-after-certain-amount-of-time) – nbro Aug 08 '15 at 11:13

3 Answers3

14

Use meta refresh tag in 404 page

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

it will redirect according to content value(in seconds) and the URL where you want to redirect

PravinS
  • 2,640
  • 3
  • 21
  • 25
  • This meta tag is deprecated. Following users should not use this anymore. https://www.w3.org/TR/WCAG10-HTML-TECHS/#meta-element – Melih Jun 10 '17 at 11:15
5

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 -->
Patartics Milán
  • 4,858
  • 4
  • 26
  • 32
3

Just add a little PHP to your 404 error page.

I assume you have this in your htaccess file, ErrorDocument 404 /error/404/

So on /error/404 you can do the following anywhere.

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

Be sure to change 5 to your seconds and wherever.html to your pat.

  • Yes I have this `ErrorDocument 404 /404.html` in my `.htaccess`. I have zero knowledge in PHP. Where should I put it? Also, the meta refresh tag has worked for me. Do I have any benefit with the php over the other one? – Sam DG Aug 03 '13 at 05:46
  • @SamDG It would be a good idea to use some php and learn some of it. Basically this runs wherever you put it in the file. php > html or html > php either way it still runs. –  Aug 03 '13 at 06:11