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.

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 = ''; 
var d=window.location.href;
d=d.replace(/.*\/\/[^\/]*/, '');
      if(d[1] < 2013 && d[1] != "p")
    var str1 = '';
   //var str2 = '/';
    var redirectto =str1;
   //to redirect to more specific url use below format
   //var redirectto = str1 + str2 + d[1];
    location.href = redirectto;

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: 
  • 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: 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 to see the page not found but if you visit you will be redirected to home page that is : 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


Popular posts from this blog

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

Checklist: Before applying for adsense