Bryan Hadaway's Blog
Your Account:   subscribe | Social: Facebook Twitter LinkedIn Google+

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:

Page Vs URL


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


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

is suggest the clean and proper:

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 — 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 and that are virtually the same page and page 2 will just canonicalize as itself? I want all portfolio pages to canonicalize as just

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


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: and subsequent pages listing projects alphabetically;, etc.

Those are distinctly separate pages that should not canonicalize to the main page 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, etc then yes they should canonicalize as

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:

Thanks for reading, Bryan

  • Copper Reflections

    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!

  • Nabtron

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

    you can check the code :

  • 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

  • Online games

    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.

  • Bryan Hadaway