BREAKING NEWS
latest

728x90

468x60

How to Redirect Automatic Error 404 Page To Homepage With Javascript

Blogboosters.net - How To Redirect Error 404 Pages To Homepage With Javascript - It could be that a blog has an error page 404 or a page not found. Usually, the page has been deleted or by other causes such as writing errors in posting URLs, and others.
 Redirect  Error 404 Page To Homepage

For that, usually a blog or web create a page or landing page specifically for error 404 page by giving a statement that the destination page has been deleted or no longer exists. The rest is given a button to the homepage or provided a search box to search for the desired post again.

Now this time I will give a trick to automatically redirect or redirect the error 404 page to the compilation so that visitors do not need to click the button to the homepage.

Actually, posts like this have a lot on Google, but there are Blogboosters friends who have difficulty implementing it because the script does not work in some templates, especially templates that use conditional tags for error 404 pages.

If you want to try it, please copy the following javascript code and save it above the code </body>or &lt;!--</body>--&gt; &lt;/body&gt;

1. For non AMP blogs

<b:if cond='data:blog.pageType in {&quot;error_page&quot;}'> <script> setTimeout(function () { window.location.replace("<data:blog.homepageUrl/>"); }, 8000); //will call the function after 8 seconds. </script> </b:if>

The above code will automatically redirect the error 404 page to the homepage after 8 seconds so that visitors can still read the message on the error 404 page, you can set/change the number 8000to set the waiting time. 1 second = 1000.

If you want to directly redirect, use the following code.

<b:if cond='data:blog.pageType in {&quot;error_page&quot;}'> <script> setTimeout(function () { window.location.replace("<data:blog.homepageUrl/>"); }); </script> </b:if>
Or
<b:if cond='data:blog.pageType in {&quot;error_page&quot;}'> <script> window.location.replace ("<data:blog.homepageUrl/>"); </script> </b:if>

Read Also: How to Make Script Boxes in Blog Posts

2. For blogs that use mobile URLs? M = 1 as AMP, use the following code that will only apply to desktop URLs

<b:if cond='data:blog.pageType in {&quot;error_page&quot;} and data:blog.isMobileRequest == &quot;false&quot;'> <script> setTimeout(function () { window.location.replace("<data:blog.homepageUrl/>"); }, 8000); //will call the function after 8 seconds. </script> </b:if>
Or
<b:if cond='data:blog.pageType in {&quot;error_page&quot;} and data:blog.isMobileRequest == &quot;false&quot;'> <script> setTimeout(function () { window.location.replace("<data:blog.homepageUrl/>"); }); </script> </b:if>

3. This can also be used in Wordpress, please save this code in the 404.php section at the bottom.

<script> setTimeout(function () { window.location.replace("<?php echo home_url('/'); ?>"); }, 8000); //will call the function after 8 seconds. </script>
Or
<script> setTimeout(function () { window.location.replace("<?php echo home_url('/'); ?>"); }); </script>

Maybe useful...

Read Also: Creating Sub Domains from Custom Domain Blogspot
« PREV
NEXT »

No comments

Facebook Comments APPID