Super Simple Dynamic Canonical Link Code

Canonical Link WordPress Plugin

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


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


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.


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:


is suggest the clean and proper:

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 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, in my opinion. Page 1 and 2 examples would only make sense being canonicalized if we were talking about: and 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 that page, not an entirely different page. The use of


would probably be more appropriate here.

However, if we were talking about ID sections like:,, etc., then yes, they should canonicalize as just:

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.