Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Code To Put Canonical Link Element In Page Template


  • Please log in to reply
11 replies to this topic

#1 Jill

Jill

    Recovering SEO

  • Admin
  • 33,244 posts

Posted 18 November 2009 - 12:02 PM

I would like to put the canonical link element on every page of a website so that Google only indexes the one-true URL.

I'm thinking there is probably some php code I could add to the header of the template that would automatically pull the "real" URL from somewhere and have this work.

What do you think? Doable? If so, anyone know what the code would be?

Or maybe it doesn't go in the header at all but the .htaccess?

I know there's a wordpress plug in that does this and I'm using that on my WP site, but I'm wondering for a non-wp site. (I will also go try to find the code on my WP site too as that might help.)

#2 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 18 November 2009 - 01:15 PM

Are you just talking about the www/non-www sort of thing Jill?

That one should be pretty easy. If you need to control the actual page names you'd have to have those possibilities loaded into a database and query it to get the correct page location info.

For www/non-www situations where no query strings were ever present you could simply use something like:

CODE
<?php
$page = $_SERVER['PHP_SELF'];
$canlink = "<link rel='canonical' href='http://www.yoursite.com" . $page . " />";
echo $canlink;
?>


If you needed to test and deal with query strings you'd want to extract it to see if it was a null value, then if it wasn't add the ? and query string info. There are lots of ways to test for null or empty variables. In this case simply checking to see if the variable was a string length of 0 should do the trick.

So a quick and dirty example of how this would look to handle both urls with query strings and those without could look something like:

CODE
<?php
$qs = $_SERVER['QUERY_STRING'];
$page = $_SERVER['PHP_SELF'];
if(strlen(trim($qs))==0)" {
  // No query string is present
  $canlink = "<link rel='canonical' href='http://www.yoursite.com" . $page . " />";
}else{
  // Query string is present
  $canlink = "<link rel='canonical' href='http://www.yoursite.com" . $page . "?" . $qs . " />";
}
echo $canlink;
?>


Of course I haven't thought this all the way through yet so there may be some other things that one might want/need to take into account. PHP_SELF will pull the complete path information from the root level, so as long as you don't have any url rewrites in effect it should work fine. There might however be issues with index.php pages (with no query strings) since I believe PHP_SELF will probably see that it's actually on the index.php page. Even if it's not present in the address bar.

Frankly, I just do it with .htaccess and issue a 301 redirect when necessary. It just feels cleaner and easier IMHO.

#3 Jill

Jill

    Recovering SEO

  • Admin
  • 33,244 posts

Posted 18 November 2009 - 01:21 PM

No not the www boondoggle giggle.gif

I'm talking about if I was using tracking URLs within the website for certain links, but I don't want the the pages to be indexed that way.

So they would all start with ? after the actual URL.

I know the easy work around is to just make my tracking links as nofollow, but was thinking this might be a nicer solution.

Although I may have found a workaround for not having been able to previously add custom code to an individual header, in which case I can do the canonicals by hand as necessary. (Won't be tons.)

#4 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 18 November 2009 - 02:23 PM

So you're be stripping off all query strings for these urls? And there won't be any cases where the query string should actually be there?

In that case it would be easy. Just test for any query string and dynamically write the link canonical header if and only if there is a query string present.

CODE
<?php
$qs = $_SERVER['QUERY_STRING'];
$page = $_SERVER['PHP_SELF'];
if(strlen(trim($qs))>=1) {
  // A query string is present, so set the link header
  $canlink = "<link rel='canonical' href='http://www.yoursite.com" . $page . " />";
  echo $canlink;
}
?>


Of course you could get a finer level of control if sometimes query strings are legitimately present and you only want to remove the tracking parameters. To do this would take a little regex statement to identify the variable names that are in your tracking urls and strip them out for the canonical <link> reference.

#5 Jill

Jill

    Recovering SEO

  • Admin
  • 33,244 posts

Posted 18 November 2009 - 06:54 PM

Would it matter if the "real" URLs (before some sort of mod_rewrite) had query strings? The ones the browser always gets don't typically have query strings. I'll double check to make sure, of course as there could be a few exceptions.

For this purpose, it's just Google campaign tracking that I'd want to strip out.

#6 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 18 November 2009 - 09:00 PM

You're not doing a redirect or anything with these, so no other things that are happening in the background with rewrites should be affected in any way. No danger of any redirect loop either.

About the worst that can happen is that some URLs end up with a <link> thingee pointing the spiders to a wrong canonical URL. And that's something that can be spot checked as soon as it goes live to make sure the script is writing what you want it to write.

#7 1dmf

1dmf

    Keep Asking, Keep Questioning, Keep Learning

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,167 posts
  • Location:Worthing - England

Posted 19 November 2009 - 06:19 AM

I'm a little confused, isn't canonical an Alias, which is set up via CName DNS records?

#8 Jill

Jill

    Recovering SEO

  • Admin
  • 33,244 posts

Posted 19 November 2009 - 08:07 AM

QUOTE(1dmf @ Nov 19 2009, 06:19 AM) View Post
I'm a little confused, isn't canonical an Alias, which is set up via CName DNS records?


There's also the canonical link element Google introduced that you can put in your HEAD. It's fairly new.

#9 1dmf

1dmf

    Keep Asking, Keep Questioning, Keep Learning

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,167 posts
  • Location:Worthing - England

Posted 19 November 2009 - 08:42 AM

OIC, thanks Jill.

So now you can tell Google when you have multiple URL's pointing to one page, without ending up with duplicate content filtering problems.

Does G! pass the PR juice correctly the same way as if you have set up 301 redirects?

Edit -> I just watched the Mat Cutts video, woohoo Jill you got a big up from Matt thumbup1.gif

But this new link attribute setting for relevancy is pretty cool, it could remove some serious headache for people, and consolodate all your PR to the one correct page.

I like it!, one thing I'd never realised was G! seeing uppercase & lowercase in file names as different urls, but technically I guess it is correct as *nix see's them as two different files, infact depending on the length of the file name, you could make a ridiculous amount of different pages, with combinations of uppercase and lowercase letters in the file name.

And as Matt points out, you cannot control how peole link to you, but if you don't have the page with the way the user has linked to you, on *nix it's a 404 error page, it never occured to me that G! would see them as different pages even though MS IIS would still find the page ok.

Edited by 1dmf, 19 November 2009 - 09:01 AM.


#10 Jill

Jill

    Recovering SEO

  • Admin
  • 33,244 posts

Posted 19 November 2009 - 09:17 AM

Well, I'm sure I didn't implement the code correctly and/or it simply wasn't compatible with the CMS so it broke things.

But the good news is I did manage to add a field to add some custom code into the HEAD so I can at least do this (and other things) on a page by page basis as necessary.

#11 1dmf

1dmf

    Keep Asking, Keep Questioning, Keep Learning

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,167 posts
  • Location:Worthing - England

Posted 19 November 2009 - 09:22 AM

I see this actually being rather difficult to implement Jill.

My site has a page which is a perl program accepting Query String vars, how do you build a dynamic canonical header link tag.

How would I correctly point the SE's to the real page but include the relevant QS vars which make the page a unique URI.

Dynamic content with acceptable QS vars for unique content could prove difficult to produce the relevant canonical link details.

hmm, this upper / lower case business is a real pain!

#12 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 19 November 2009 - 11:08 AM

If you were using the last example, there was a typo in there that was probably messing things up Jill. An extra " jumped in there somehow or the other. I've fixed it now if you want to try it again.

They don't call me the King of Typos for nothing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

We are now a read-only forum.
 
No new posts or registrations allowed.