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://joostschuur.com/ Joost Schuur

    That post suggests modifying core WordPress files that get overwritten when you do your next upgrade, which is why putting it in the theme files made more sense to me.

    In the end, I went with a feature of the HeadSpace plugin, which I’m already been using, added a ‘raw data’ module and then in the home page settings for that plugin added ” for the raw data field. This at least covers my original intent, getting my front page ‘fixed’.

    That said, for 3.0 users there’s still the need to do this in a nice and simple way, and for some people HeadSpace might be a lot of overhead. It really comes down to whether duplicate canonical links are going to harm you down the road.

    Quickly disabling WordPress’ own canonical links programmatically still leaves it a lean and mean plugin with no visible user configuration.

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

    Yeah, I definitely DO NOT recommend having duplicate canonical links. I’m sure Google would just go to the first one, but I don’t know what unforeseen problems this could have either.

    So, I would definitely recommend choosing either the WordPress default canonical solution or a canonical plugin. I don’t believe WordPress versions prior to 2.9 even have a default canonical link element, so that isn’t a worry. For newer versions, I believe there is a plugin to remove the default canonical and then it wouldn’t get in the way of updating.

    I know there were some, but I’m not seeing any good ones, I may consider developing a “Remove Canonical” plugin.

    It’s worth noting too that I just noticed another plugin, that can be very useful to assist canonical links:

    http://wordpress.org/extend/plugins/wp-htaccess-control/

    Thanks, Bryan

  • http://joostschuur.com/ Joost Schuur

    A separate plugin to remove canonical links would be a great idea. That way, people could use it regardless of what other solution to add canonical URLs to their pages they’re implementing, and it would be theme independent. It sounds like the easiest plugin in the world to write too, just a step up from Hello Dolly, since it just comes down to a single remove_action call. I might even try my hand at it, as a way to get into plugin development. If you’re not going to release one, I can do that.

    I noticed that in my instructions above, the actual appropriate canonical link tag was stripped out because the comments area interpreted it as HTML. So where I said ‘and then in the home page settings for that plugin added ”’, don’t add a blank string of course, add the correct canonical link tag for your home page.

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

    Yeah, I’ve even written an article that will help jumpstart you on building it if you decide to:

    http://www.bryanhadaway.com/getting-started-with-your-first-wordpress-plugin/

    If you could… let me know by tonight or I’ll go ahead and make it tomorrow.

    Thanks, Bryan

  • http://twitter.com/tomsyweb Ismael Toé

    Great! But if you have for example “http://www.website.com/coding/”, it can’t provide “http://www.website.com/coding” and vice-versa, when “coding” is a file (for example coding.html or coding.php renamed to coding via htaccess).

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

    Actually, that’s a good thing. You don’t want to “lie” to Google. If this is really how the page lands; “http://www.website.com/coding”, then that is how it should canonicalize, because if you set the canonical to: “http://www.website.com/coding/” it will always be a redirect from Google although Google might ignore the suggestion altogether anyways.

    The main structure of a URL that the users sees with the hash # and query ? tags cut out of course should be how the page canonicalizes.

    It can and does create the right canonical link for your scenario. I’ve incorporated the canonical link code I’ve written on this page into a free website template I offer here:

    http://www.freewebplate.org/

    where I am actually practicing stripping off .php for “prettier” URL’s in .htaccess, see the demo:

    http://www.freewebplate.org/demo/contact

    And you’ll see that it respects the removal of .php and the trailing slash /.

    Basically, what my code does is strips out hash # and query ? tags which is about 90% the importance of what a canonical link is for.

    Other than that it pretty much respects what it sees, so like any good canonical link usage/practice, whether mine or another script, it is also very important to have a good .htaccess file and overall good link structure and consistency in your code.

    One important and often overlooked factor in websites is whether or not to be a www or non-www site and then to stick to it strictly.

    Let me know if that clears some things up Ismael, or perhaps I misunderstood or maybe you can expand on why what you were suggesting might have a good/useful application.

    Thanks, Bryan

  • http://www.seo-julie.co.uk Julie

    Hi Bryan, thanks for this. I want to be able to dynamically “noindex,follow” certain pages on a non WordPress site, how can I do this?

  • Compit

    Hi Bryan,,,
    I tried your plugin to for a property websiting, which is using “wp-property” plugin,
    it did not work, I was worried about messing it all up but I change the premlinks back to default and left your plugin active, its not doing any damage now, I am look ing to deactivate as its of no use now,
    The Wp-Property plugin,, claims that it will generate SEO friendly urls, that may the problem.,,,but it doesnt work for pages and post outsite the plugin, !!!!
    let me know if you tested this set up and you may have some tips or tweaks you can do, as I do want all the pages to have SEO friendly urls,,

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

    @Julie – Actually, there is an excellent plugin to handle this I never thought of sharing before, thanks for asking. Check it out:

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

    @Compit – I understand you may need to or want to use WP Property for a certain feature or two, but could you disable it’s handling of URL’s and let permalinks and canonical links do their job? Some plugins you can get to play nice together if you finesse the settings just right, others will conflict.

    Thanks, Bryan

  • http://www.seo-julie.co.uk Julie

    Hi Bryan thanks, I’ll look into that! How about for non-Wordpress sites, how can I create a piece of code that will deal with dynamic pages?
    Thanks, Julie

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

    The code provided in the top of this page is a universal website code, and then the plugin is for WordPress.

    Thanks, Bryan

  • http://www.seo-julie.co.uk Julie

    Hi Bryan, I just used the dynamic canonical code, but it seems to create a canonical tag for every page as it’s pulling the entire URL into the tag.

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

    Yes, that’s correct. Is this for some reason not desired? You could write in conditional tags if there are pages you wish to exclude from canonicalization.

    Thanks, Bryan

  • http://www.mijncomputercursus.nl/blog/ Tony

    Hi,

    Is it normal that the canonical for an article appears in the index too? Because now it says: the right url for the index is the link of the article (the c-link of the newest article).

    It also appears in archive and categoriepages.

    The index, archive and category pages must have an own canonical right? But now it gives the Canonical for the article wich is first on the page…..

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

    Hey Tony,

    Can I get a link to the website in question while the canonical link code is active?

    Also, if the site is a WordPress site, what version are you using?

    Thanks, Bryan

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

    I ended up helping Tony via email.

    It turns out he was using the “Canonical URL’s” plugin that was throwing errors.

    My plugin “Canonical Link” still works fine; just tested on WordPress 3.0.4.

    Thanks, Bryan

  • mccteam

    I don’t need the other plugins: Homepage Canonical Link and Remove Default Canonical Links besides yours?

    Is it good/enough to place the canonical on the article page self? Because I read about the canonical has to be placed on every page where the article is. And it makes it more difficult when a few articles are in 1 page like archive and categories. Then you use noindex for that pages i guess..

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

    Hello again,

    You should be fine just using my plugin. If when you’re using only my plugin you get two canonical links you should use Remove Default Canonical Links as well.

    My plugin simply needs to be activated and it will automatically insert the code into ALL pages and posts.

    If you wish to only have articles indexed with Google you could use a sitemap plugin and only choose posts to be included in the sitemap.

    The surefire way to block pages from being indexed is indeed noindex:

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

    is a good plugin. Be sure and let me know if you need any more help.

    Thanks, Bryan

Feedback
x close