HackingUniversity - Hacks . Tricks . How-To's

22 October 2012

Automatically Redirect Blogger 404 Error Pages to Homepage

404 error pages are normally created when ever you delete out any old post or you just update the posts changing the slugs so its old url just returns a error page. Well in blogger you are not able to change any slugs but if you delete your old post either you can redirect that page manually to your any other post by using the search preferences option officially in blogger or you can use the below script that will automatically redirect all your 404 error pages on blogger to your homepage. So if anyone has linked to your older post and he clicks on that link now if will not see any blank page instead of that he will be redirected to your homepage for some fresh content.

Redirecting 404 Pages using Blogger Custom Redirect

Well if you wanna redirect only few 404 pages than you can use the official custom redirect feature available in blogger that can solve out your problem. It can be used to redirect any older page to the newer updated one or any other blogger post.

blogger-custom-redirect

  1. Visit Blogger > Settings > Search Preferences > Custom Redirects.
  2. Now just click on edit and and press New Redirect and enter the old URL and your New URL on which you wanna redirect.
  3. Now check mark on Permanent and press Save.

Redirecting All 404 Pages using JavaScript

If you do not want to do redirect every page manually than I think that you should use the below JavaScript that will automatically redirect any 404 error page on your website to your homepage or any other website. You just need to paste the code in your template and everything works on its own.
  1. Visit Blogger > Template > Edit HTML
  2. Now click on Expand Widget Templates and press Ctrl + F and search for <data:navMessage/> or <b:if cond='data:navMessage'>
  3. Now paste the below code right below the above code you just found.

    <b:if cond='data:blog.pageType == "error_page"'>
    &lt;script language=&quot;javascript&quot;&gt;
    window.location=&#39;<data:blog.homepageUrl/>&#39;;
    &lt;/script&gt;
    </b:if>

  4. That’s it now just save your template and refresh any of your 404 error page to check if this script is working or not.
  5. The above is demo can be used as your demo too just change my domain name with your and see how that url will be redirected to your homepage automatically.
  6. Note Do not change that Sorry line else script will not work, that's the line blogger officially uses and that's what we are tracking and redirecting.

Now if you do not want too redirect your 404 pages to your homepage instead you want to redirect to some another website than just replace the blue line in above code with http://xxxxxxxx.com and that’s it will start redirecting your error pages to that domain name.

So I think this will pretty useful fro anyone who has many 404 error pages like my website as I faced some severe problem so I just have to remove many posts that are now 404 error pages so I have installed this script and its really a relief.