Dealing with canonical links

When using Google SItes, the 'official page' may not match your domain name where you are using redirection and other DNS tricks to point your domain at the site content. 

So for example, the official Sites Url for this page is 

https://sites.google.com/a/mcpher.com/share/Home/excelquirks/analyticsandsites/canonical

But the typical way it will be accessed is 

http://ramblings.mcpher.com/Home/excelquirls/analyticsandsites/canonical

So we need to normalize the target page URL so that we remain within the same domain that we are currently operating under.

function selectLink (item,sUrl) {

// try to make the favorite match access mode / redirection

var target = item.analytics[item.analytics.length-1].favorite;

// extract the current domain

var current = document.referrer.toString();

var currentDomain = /(^http[s]?:\/\/[\.\w]+\/)/i.exec(current)[1];


// extract the target domain

var targetDomain = /(^http[s]?:\/\/[\.\w]+\/)/i.exec(target)[1];


// manipulate the target domain to match the current one.

return targetDomain === currentDomain ? target : target.replace(sUrl,currentDomain);


}


For help and more information join our forum,follow the blog or follow me on twitter .


Comments