Super Simple Dynamic Canonical Link Code

Canonical Link WordPress Plugin

Download Now | Find on WordPress.org | 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.

The canonical link represents the official URL of a specific piece of content. It also helps 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 override 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 (supporting PHP) without any alteration to it needed. Just paste it directly in between your

<head> ... </head>

tags, which are hopefully located in one 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 in Firefox when using the recommended add-on: SearchStatus.

Learn More About the Canonical Link Tag Element

First, pay visit to and learn from the true authority on the subject: Google on Canonical URLs.

Page Vs URL

Page

A page is where the text, pictures, and other content actually display.

URL

The URL (Uniform Resource Locator) is the location of a page. In the old days of web design where every page was manually created as a static HTML file 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 search and other parameter results because of so many URL variables confusing the bots.

FAQ

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

No. 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/

Again, this script will not just mirror the current URL (unless you’re already at the canonical URL). The list goes on and on how just one page can be shown and reached via different URLs. 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, in my opinion. 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 that page, not an entirely different page. The use of

noindex

would probably be more appropriate here.

However, 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 just: 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.

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 following tag:

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

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 page’s canonicalized state (if it contains a canonical link), you need to use this plugin 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 homepage, 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

  1. From your WordPress Admin, navigate to: Settings > Permalinks
  2. Under “Common settings”, select: “Custom Structure” and paste in: /%postname%/
  3. 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?

Need help? Comment below or post in the forum.

  • Thank you Bryan, for checking it. I do not know much about the codes. Somebody made it 5 years ago and he disappeared.
    Thank you very much for your time.

    Do you mind remove that contact us link please? It will probably come out as a not found in my Google webmaster tools. LOL!

  • I’ve modified the functions.php to show the custom canonicals without using any plugins or so, all manually,

    you can check the code :

    http://nabtron.com/manually-canonical-wordpress-permalinks-without-plugins/8177/

  • Excelanto

    Thanks for your valuable posting.I have collect more than information from your website. It is really wonderful blog. please added more than tips. i’m working in Erp software in india.Here providing very low price and Quality ERP,Cloud ERP, CMS , responsive webdesign and ERP. you have any more than information kindly make me call this number 044-42127512 or send your mail info@excelanto.com.

  • Hi ,my website is not in php has smarty template wich is .tpl how can I add canonical based on browser link.I want to add it to all pages as the script is an game arcade.Thanks in advance.

  • Hi Bryan,for smarty template I want to show canonical ,I tried the code but not working,then I tried to show php code like this {php} and close with {/php} still not working,any sugestion,thanks in advance.

  • Sorry, I’ve never used Smarty before.

    You might have better luck asking the folks over at:

    http://www.smarty.net/forums/

    Thank you.