Bryan Hadaway's Blog
Signup for the Best Hosting Around

Super Simple Dynamic Canonical Link Code

Canonical Link WordPress Plugin

Download Now | Find on WordPress with Instructions | Help Forum

Canonical Link Script

The importance of the canonical link is primarily to clear up duplicate content issues. That is, to help search engines not make the mistake of confusing two different URLs that are actually the same page as two different pages.

It doesn’t help if you actually have duplicate content. Also, to give Google a clean representation and suggestion of what you want your URLs to look like and how you want Google to index and display them in search results.

It’s important to understand that the canonical link is not a rule, but merely a suggestion. Google (and other search engines) can still overrule your suggestion. It might have been better named Canonical URL Suggestion.

Adding a canonical link tag to every page of a website, one by one for most is severely daunting if not completely impractical or impossible. Especially if you have thousands of pages.

So without further wait, this code is ready to go as is for any website without any alteration to it needed, just paste it directly in between your:

<head> ... </head>

tags which are hopefully located in one dynamic/universal header file that governs all your pages or you’ve got a lot of work on your hands ;):

<link rel="canonical" href="http://<?php echo $_SERVER["HTTP_HOST"] ?><?php echo parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH); ?>" />

For a live demo, this very page is using the code. Reference the little “C” in the URL bar in your browser. (in Firefox using the SearchStatus add-on)

Learn More About the Canonical Link Tag Element

First, pay visit to and learn from a true authority on the subject:

http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=139394

Page Vs URL

Page

A page is the actual content where wording, pictures and videos are displayed.

URL

The URL is the location of that page. The old days of web design where every page was manually created as a .html are long gone. Because there are now so many different technologies passing info along through the URLs of websites is why the need for canonicalization was born. To help search engines understand actual pages vs false pages because of so many URL variables confusing the bots.

Common Misunderstanding with My Script / Plugin

All it is doing is serving up the current page URL as the canonical link?

Untrue: What the dynamic canonical link tag script will do for all these variations:

http://website.com/ | http://www.website.com/ | http://website.com/index.php | http://www.website.com/index.php | http://website.com/#section1.2 | http://www.website.com/#section1.2 | http://website.com/?=source=twitter+is+cool&id=2654683

is suggest the clean and proper:

http://www.website.com/

This script will NOT just mirror the current URL. The list goes on and on how just one page can be shown and reached via different URLs. Again, it will not just mirror the current URL. UNLESS you already reached the page via the cleanest URL http://www.website.com/ — there’s nothing to worry about when that happens. That’s in no way an error, issue or problem. It’s just fine.

Search engines can’t fully make certain distinctions (as in can’t make human based distinctions). They can consider some of those URLs to be different pages. That is why canonicalization was created, to help clear up duplicate page issues because of the many possible URL variations.

“But, I have http://www.example.com/portfolio/page-1/ and http://www.example.com/portfolio/page-2/ that are virtually the same page and page 2 will just canonicalize as itself? I want all portfolio pages to canonicalize as just http://www.example.com/portfolio/

That’s simply not good practice. Page 1 and 2 examples would only make sense being canonicalized if we were talking about:

http://www.example.com/portfolio/?id=page-1

and

http://www.example.com/portfolio/?id=page-2

or something like that.

Otherwise, what that would be suggesting would almost certainly be overruled by Google’s judgment. It’s not good practice to completely canonicalize one page as another.

Say you had a projects page: http://website.com/projects/ and subsequent pages listing projects alphabetically; http://website.com/projects/a/, http://website.com/projects/b/ etc.

Those are distinctly separate pages that should not canonicalize to the main page http://website.com/projects/. That’s not the purpose of canonicalization. If it’s a page it’s a page, bottom line. The point is to canonicalize to the cleanest, proper default URL for a THAT page, not an entirely different page.

So, if we were talking about ID sections like http://website.com/projects/#a, http://website.com/projects/#b etc then yes they should canonicalize as http://website.com/projects/.

It’s your job as a webmaster to not create two fully different pages that are exactly or basically duplicates. Remember the canonical link is not a rule and does not necessarily enforce anything, it is only a suggestion.

You as a webmaster should also never rely solely on the canonical link. You need to be doing everything else right too. You need a good .htaccess file and your own internal link structure should be consistent and show URLs how you want them to be shown and so on.

And for the case of actually wanting or needing to have two or more exact (or close to) duplicate pages and don’t want them all indexed the proper method would actually be to use the:

<meta name="robots" content="noindex" /> tag.

So hopefully, that clears up some of the misconceptions about the canonical link element. It is not a lone wolf, it must work together with a pack of SEO techniques.

Verify Canonicalization is Working

To get the “C” in Firefox to show the status of a pages canonicalized state if it contains a canonical link you need to use this plugin: SearchStatus and set the canonical link button to show from the options.

This will help you confirm if and how you have your canonical links set correctly.

- No canonical link detected; no C in the address bar

- Currently on correct canonical version of a page URL; grey C in the address bar

- Not currently on correct canonical version of a page URL; blue C linked to the correct URL in the address bar

This is also my favorite tool for real-time PageRank detection on any page, there are a few other SEO related uses for the plugin as well.

Other Plugins to Assist in Canonicalization

Homepage Canonical Link – Adds a canonical link to your blog’s home page, which the current WordPress does not do. – Thanks Joost Schuur (may no longer be needed for new versions of WordPress)

Remove Default Canonical Links – Disables WordPress’ default generation of canonical links. Useful if you’re generating your canonical links through other means. – Thanks Joost Schuur (may no longer be needed for new versions of WordPress)

A Good .htaccess File is Also Advisable

Besides keeping your internal link structure and navigation clean and consistent from the get go, you can further ensure that people reach and link to clean, well structured URLs with an .htaccess file. You can find an example of one here and a dynamic one here.

Here’s a WordPress plugin to help with .htaccess management.

How to have SEO-Friendly URLs in WordPress

  • From your WordPress Admin, navigate to: Settings > Permalinks
  • Under “Common settings“, select “Custom Structure” and paste in: /%postname%/
  • Click “Save Changes” and you’re done

Not Happy with the Plugin? | Let’s Make it Better

What does the plugin not do right? | What would you like to see the plugin do?

Let’s get the suggestions and questions going in the forum:

http://bryanhadaway.com/forum/canonical-link/

Thanks for reading, Bryan

Other Reads:

Tagged in , , , , ,

Want to share your website, writing, product or service with all my readers too?  
  • http://bennykusman.com Benny

    Hi Bryan,
     
    I have installed this canonical link, but it doesnt appear ‘C” on my web.. can you help to take a look what’s wrong with the installation?

  • http://www.calmestghost.com/ Bryan Hadaway

    Hi Benny, I’ve checked http://bennykusman.com/ for you and you’ll be happy to hear that it is working perfectly.

    The “C” is a feature native to Firefox which I recommend using anyways:

    http://www.mozilla.com/?from=sfx&uid=266924&t=318

    Thanks, Bryan

  • http://indiapoint.net Anup

    I had to deactivate this plugin because, I found that the very purpose for which this plugin was used got defeted, as it simply echoed the URL as canonical. For example

    If the canonical URL is: “http://abcanylink.com/anything/”

    and the link in the URL is: “http://abcanylink.com/anything/?utm_source=twitterfeed&utm_medium=twitter”

    then using this plugin, the canonical URL shown in the header is: “http://abcanylink.com/anything/?utm_source=twitterfeed&utm_medium=twitter”

  • http://www.bryanhadaway.com/ Bryan Hadaway

    Hi Anup,

    You can manually enter the code found above into the head section of your header.php file and it should work perfectly for you. Working on an update for the plugin.

    Thanks, Bryan

  • http://www.bryanhadaway.com/ Bryan Hadaway

    Update: Canonical Link 1.1 is now available:

    Download Now | Find on WordPress with Instructions

    Thanks, Bryan

  • http://blog.milandinic.com/ Milan

    First of all, I don’t understand why you at a first place needed to create this functionality since it is included in WordPress core since version 2.9.

    Second, this is definitely not a right way to make canonical URL what you are using in code.

    So I do not recommend use of this plugin.

  • http://bennykusman.com Benny

    Ya, i had the same idea. Im not too sure what is the purpose though it looks good at the introduction… i already deactivate it…. probably Bryan should elaborate the purpose for this plugin more.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    Well, I think there’s no argument there, if you don’t care about or want canonical links in the first place, there’s no reason to look into this plugin and of course it wouldn’t be useful to you.

    You’re correct, in the newest versions of WordPress, canonical links are already in place, but this plugin covers WordPress versions as early as 1.2 which aren’t covered. Also, it’s worth mentioning that the WordPress native canonical link itself is flawed. It fails to even properly canonicalize home pages, this might be fixed in WordPress 3.0.

    There’s no doubt that entering canonical links manually page by page would be the most “accurate” and ideal way to go about it.

    But, as we know… if you have hundreds of pages that is definitely not practical. To elaborate more, the purpose of canonicalizing a link is to serve up to search engines (Google, Yahoo, Bing etc.) a cleaner more attractive, seo-friendly and user-friendly version of URLs.

    Another important factor to consider is duplicate content; the same page, could be indexed several times in different formats and Google’s crawler could fail to understand that’s just one page, because the same content can be reached with so many variables;

    “http://www.website.com/”, “http://website.com/”, “http://www.website.com/index.php”, “http://website.com/index.php”, “http://www.website.com/#section1.2″, “http://www.website.com/?=section1.2&yes&on”

    It will suggest: “http://www.website.com/” for that page

    It strips off hash (#) and query (?) tags. It’s important to keep in mind that a canonical link does not actually change the browser URL or alter the page in any way, it only serves a suggestion for a cleaner URL to Google.

    The Bottom Line:

    My canonical link code/plugin is the best free, dynamic and hands-free way to implement canonical links into large-scale websites and WordPress blogs that I’m aware of. It can not hurt your website, it can only help it.

    I emphasize the words dynamic and hands-free because there is another canonical link plugin to choose from: http://wordpress.org/extend/plugins/canonical/ that might be better suited for you if you’re willing to set every single page and post canonical link by hand. It’s worth noting that it only works with WordPress 2.9+ and that it only applies to pages and posts and not the rest of your WordPress website.

    The best full on method to get clean URL structures is a good combination of setting your Permalinks to Custom Structure: /%postname%/, using a good .htaccess file and canonicalizing links.

    So if you don’t like it or you don’t think it works the way you think it should don’t just say that and leave it at that, that helps nobody. Get specific; What does the plugin not do right? What would you like to see the plugin do?

    Let’s get the suggestions and questions going in the forum:

    http://www.bryanhadaway.com/forum/canonical-link/

    Thanks, Bryan

  • http://indiapoint.net Anup

    First of all, I don’t understand why you at a first place needed to create this functionality since it is included in WordPress core since version 2.9

    Is it so, where is that in the admin panel? Or does it get generated ? I dont find it in the headers though.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    @Anup

    It should be generated in the default theme automatically, I’m not entirely sure.

    I think it might only present itself on a “needed” basis. But, I just tested my blank WordPress 2.9.2 w/Default Template setup that I use for testing and I tried adding #test and ?test to URLs and I couldn’t get it to prompt a canonical link.

    Which brings up another scenario of when a canonical link plugin could be useful… because even with the newest version of WordPress, it might be theme dependent if it’ll work or not because it’s served up in header.php.

    I don’t think it does anything different than the plugin anyways, so I’d be interested to hear specific examples of problems. I did already update the plugin based on your suggestion Anup, so I am listening.

    Thanks, Bryan

  • http://indiapoint.net Anup

    @Bryan. The point really is what exactly is and most likely to be considered a canonical link?

    And I give here an example from my website:

    Say the link: “http://indiapoint.net/ca/2006/02/27/articleship/”

    Now the above post has various comments to it. So, for smooth loading, I have put around ten commnet threads to be shown.on each page.

    So, there are different pages that gets generated viz

    “http://indiapoint.net/ca/2006/02/27/articleship/comment-page-5/”
    “http://indiapoint.net/ca/2006/02/27/articleship/comment-page-4/”

    etc.

    Now, if there is only one canonical link, the above pages would have the canonical link of the original post viz. “http://indiapoint.net/ca/2006/02/27/articleship/” So that would defeat the purpose.

    However at the same time, there can be search links like “http://indiapoint.net/?s=training” So what would be the canonical link for such links?

    At the same time, I would not like that search-engines pick up from somewhere link like “http://abcanylink.com/anything/?utm_source=twitterfeed&utm_medium=twitter” and count it as a link.

    thanks

  • http://indiapoint.net Anup

    I have written a reply, but it seems that your spam protection is working overtime.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    It’s because of all the URL examples we’re using that’s sparking the spam filter, but I approved your comment and my response is above.

    Thanks, Bryan

  • http://www.bryanhadaway.com/ Bryan Hadaway

    Here’s how the plugin will handle all these pages:

    “http://indiapoint.net/ca/2006/02/27/articleship/” (no suggestion needed, no suggestion will occur)

    “http://indiapoint.net/ca/2006/02/27/articleship/comment-page-5/” (no suggestion needed, no suggestion will occur)

    “http://indiapoint.net/ca/2006/02/27/articleship/comment-page-4/” (no suggestion needed, no suggestion will occur)

    Each of these pages will be respectively left alone, no suggestion will be needed. They will all be indexed the way they are as they should. You should want comment pages to be indexed because there are often rich info filled comments that can bring in visitors.

    “http://indiapoint.net/?s=training” canonicalized as “http://indiapoint.net/”

    This is good, it’s not really good practice or appropriate to index search pages anyways as this annoys most, to go from search results to more search results.

    “http://abcanylink.com/anything/?utm_source=twitterfeed&utm_medium=twitter” canonicalized as “http://abcanylink.com/anything/” respectively

    Canonical links again are mere suggestions for Google. It’s an added technique, but not the only technique to consider, a good .htaccess file and seo-friendly permalinks are also advised.

    If ever you didn’t want a page to be indexed, the proper method would be to use:

    <meta name="robots" content="noindex" />

    in the head section. Here’s a WordPress plugin that could handle such a task:

    http://wordpress.org/extend/plugins/ultimate-noindex-nofollow-tool/

    I have to think about making the plugin universal or basic to cover the average need. There may certainly be other needs, this plugin could be customized very easily to someone’s specific need.

    For example, perhaps some pages; you might want to block from being canonicalized altogether or changing how URL’s are canonicalized for specific pages. Right now, the plugin is at a pretty fair baseline of what should be expected from a canonicalized link.

    All and all for people that want to canonicalize hundreds or even thousands of pages dynamically without doing it manually themselves or paying someone, it’s pretty decent. It might not get every page spot on of how you would do it manually, but it’ll certainly do a good job with most and better your site.

    Again, it’s completely free an open source, if someone has a specific request for their own custom setup I could try and help, if I couldn’t I’m sure they could hire a programmer for 1 – 2 hours that could refine it to their specific websites needs.

    Right now, I’m thinking it’s canonicalizing pretty good in most cases no differently than how someone would manually do it, so the only thing I’m thinking is that there might be pages that people just don’t want to canonicalize at all for whatever reason, so I’ll look more into that, shouldn’t be too difficult.

    I’m also thinking of an alternate that might handle product pages a little differently, but I need more feedback first, lots of feedback. GOOD feedback like you’ve provided Anup and I appreciate that. When people just say they don’t like it or it doesn’t work, that’s pretty vague and subjective and doesn’t do anything in way of helping.

    I started a subforum here for suggestions: http://www.bryanhadaway.com/forum/canonical-link/suggestions-for-improvement

    Thanks, Bryan

  • http://indiapoint.net Anup

    I have re-implemented your plugin (perhaps it is an updated one downloaded from your site). Now it works really well – and what I had written previously regarding its deficiency no longer exists. thanks.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    Hi Anup, that is wonderful to hear. Yes, you’re probably using the updated ver 1.2. You’ve been very helpful, if you have any other requests for features or suggestions please always feel welcome to share your feedback.

    Thanks, Bryan

  • http://joostschuur.com/ Joost Schuur

    Bryan, I’d recommend some clarification in your plugin description for WordPress 3.0 users. Aside from canonical links not being used on the blog front page, you mentioned something about WP’s own implementation having flaws. I too noticed the utm querystring junk to my front page and wasn’t sure if it was worth installing a plugin for just the front page, when I knew WordPress 3.0 already managed it for multiple pages.

    If however, 3.0 has other canonical URL bugs beyond not covering the front page, then it seems like more of a clear cut choice to use the plugin.

    Of course, going the plugin route is a safe way to keep custom changes out of your template files and also means it’s a simpler choice for people not comfortable editing them.

    The site in question where I implemented it is http://iosdevgoodies.joostschuur.com BTW.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    The canonical link has definitely been improved in WordPress 3.0. However, I don’t think it covers every single page like the plugin does. Instead is only set to cover pages that might require it out-of-the-box like post pages that have #comments sections and so forth.

    I think this plugin is just about perfect for 90% of WordPress users. But for those that have used WordPress for CMS for a serious store/shopping cart e-commerce website with thousands of products, they should definitely have a professional programmer alter the PHP of the plugin and create a custom .htaccess file, but websites of that scale are probably on top of it anyways.

    I don’t think I ever want to create a version of the plugin that required any settings adjustments from the admin. I just want it to be activate and done. For those who have very specific custom needs I want to get problems and solutions going in the forum:

    http://www.bryanhadaway.com/forum/canonical-link/

    Thanks, Bryan

  • http://joostschuur.com/ Joost Schuur

    I wanted to post this in the suggestion forum, but was not able to log in there. It said there was an account with email address, but the password reminder option gave me an error.

    My suggestion was related to duplicate canonical link entries that your plugin now generates, since WordPress does its own. From another blog post, someone recommended adding remove_action( ‘wp_head’, ‘rel_canonical’ ); to your theme’s function.php to unregister WP’s default canonical link. That seemed sensible, but didn’t seem to work for me in the TwentyTen theme. You may want to consider integrating this approach into your own plugin when you get it working.

  • http://www.bryanhadaway.com/ Bryan Hadaway

    I just reset your password, check your email. I think with 3.0 the two canonical link codes shouldn’t play together, so if you decide to remove the default canonical link element from WordPress, for earlier versions I suspect your method would work, but for 3.0+:

    http://sageblogger.com/tip-for-datafeedr-users-removing-rel-cannonical-why-and-how/

    Let us know if this solution works if anyone’s tried it.

    Thanks, Bryan

Feedback
x close