Redirecting all old url to home page or any other page in blogger using script

Adding a 301 redirect for every url which has year older then the value we are setting in our script:

So why this was needed ?

Recently I found in google search console that lot of Page not found errors are triggering for my site and when I looked at the links it was clear that the errors are because the domain was owned by someone else before I got it and google crawlers were still trying to find posts and pages which were published by previous owner.

Although there is a option to create a redirect for links in blogger dashboard but doing that for every single url each and every time was too much of work and also this was a blind game as we don't know for which url the error will trigger next until it was being logged in google search console: So here is quick summary for what I did followed by script and the Long explanations.

Quick Summary: Get current Url user is visiting on my site>Extract Year part from it>If YearFromUrl(is less then)<YearIStartedWritingPostsOnMySite and also if its not page>Redirect to home url else do nothing.

Where to add this script ?


In the Javascript gadget which you can find from Layout section of blogger dashboard if not there  install it from the same page using add gadget buttons.

Note:
Make sure you edit the year part where I have mentioned 2013 and the site name to your site name in this part:
var str1 = 'http://www.tcbin.com'; 
<script>
var d=window.location.href;
d=d.replace(/.*\/\/[^\/]*/, '');
d=d.split('/');
//alert(d[1]);
//console.log(d[1]);
if(d[1])
{
      if(d[1] < 2013 && d[1] != "p")
{
    //alert(d[1]);
    var str1 = 'http://www.tcbin.com';
   //var str2 = '/';
    var redirectto =str1;
   //to redirect to more specific url use below format
   //var redirectto = str1 + str2 + d[1];
    //alert(redirectto);
    location.href = redirectto;
 }
}
</script>

Logic Explanation:

So here is the logic of my script that I thought and implemented to do this task in bulk automatically for me and prevent unnecessary page not found errors:
  • Find the year from the url of post every time a user makes a request/visit to any post on my site, as blogger links follow this nomenclature for url: YouDomainName.com/Year/Month/Link-Text.html 
  • If the Year is older then the Year I started publishing posts then redirect it to my home page(you can redirect to any other link of your choice if you want) and if not let user visit page normally.
  • Other condition was to handle the pages which means what if the link which user is visiting is a blogger page instead of post which follows this nomenclature: YouDomainName.com/p/Link-Text.html so I added a condition to do nothing using !="p"
  • Meaning the only links that gets redirected to home page are the ones that you didn't created(the ones from old owner of domain),and the ones you created will have no effect or redirection and will work normally.   
How to Test:
  • You can visit  tcbin.com/2013/SomeUrl to see the page not found but if you visit tcbin.com/2012 you will be redirected to home page that is : tcbin.com as the condition that I have is : Year<2013.
  • I have included commented alerts and console sections(// parts) in script which you can uncomment to find whats happening
  • You can improve the logic and modify it to suit your needs to get more specific redirection

Comments

Popular posts from this blog

Enabling fixed Sidebar for blogger Contempo template for screen sizes smaller than 1440px

Adding Home Older Newer Post Buttons in new Blogger templates such as Contempo

Adding copy to clipboard button to every google code prettify pre blocks in blogger