Automatic & Manual Canonical URL Links In Drupal

With an automated Canonical URL system or module in Drupal you can reduce the duplicate content Google sees by telling it where the "real" page is.


Currently there are a few options in in which you can approach canonical tags:

  • Manual Insertion within your theme templates – You’ll have the most control over unique scenarios with this one
  • Global Redirect Module – You’re probably already using this if you’ve made any attempt at SEO in the past via modules.
  • Canonical URL Module- Allows automated or manual control.
  • Nodewords Module- Like Canonical URLs but provides other SEO meta tagging options.

Manual Insertion of Canonical URLs

I find this method the quickest and most customizable if you’re familiar with a bit of PHP code.

  • Its quick and automated for all pages.
  • You can disable canonical tags or modify it on unique pages.
  • Manual modifications can effect single pages or entire directory paths (examples below).

Put the below code into your themes template.php file and note the following points.

  • If you’re already using the preprocess_page function, just paste the middle two lines into the existing function towards the bottom. If not make sure you change “themefoldername” to the name of your theme.
  • Also change to your domain.
  • Get rid of the WWW if your server configuration doesn’t use it.
  • When specifying the URL path you’re checking against use (or don’t use) a trailing slash depending on what your server is configured to.
function themefoldername_preprocess_page($hook, &$vars) {
    $urlpath = strtok($_SERVER['REQUEST_URI'],'?');
    $vars['head'] .= '<link rel="canonical" href="'.$urlpath.'" />';

Notice above, I’m breaking off any GET variables that are in the URL string when setting the urlpath. Remove the strtok() function if that is not your intention.

This next example allows for some fine tuning if you want to exclude a few pages. You can reverse it to make it an include based statement by removing the exclimation points. It checks to see if the URL path is an exact page match for /dir/pagetitle or /dir/dir/dir.

if ($urlpath!== "/dir/pagetitle" && $urlpath!== "/dir/dir/dir") {
    $vars['head'] .= '<link rel="canonical" href="'.$urlpath.'" />';

It WILL add your tags to this page: /dir/dir/dir/pagexyz because those are specific page exclusions.

So, if you want to reference an entire directory path we’ll use a regular expression with preg_match(). This time we’re only putting canonical tags on pages within the path /dir/dir/*. You can make it an excluded path by putting an exclamation in front of the preg_match.

if (preg_match('/\/dir\/dir\/(.*)/', $urlpath)) {
    $vars['head'] .= '<link rel="canonical" href="'.$urlpath.'" />';

Then chain a bunch of these together with a Switch/Case or IF statement to get exactly what you need for your custom website. Most will probably be happy to automate the process with one of the modules below. It really depends how large and sophisticated your site is.

Worst case you might end up between two evils. Wanting most of your site to have automated canonical tags but have several (maybe 50+) individual pages you’d have to specify. In that case use the above method and build in a CCK field for your canonical tag. If that field on any particular node is filled out, substitute for that.

Global Redirect Module – “Add Canonical Link”

Global Redirect provides a simple checkbox, which right now is only available in the dev version of Global Redirect (6.x-1.x-dev).

Global Redirect is available in Drupal 5, 6, and 7 which fits the broadest amount of users. I’m a bit weary of using dev versions of anything. Still, this one seems to work fine for me.

Canonical URL Module

The Canonical URL module allows a few more details to be set. Here’s what you should know:

  • Only available for Drupal 6.
  • Automatic and Manual Mode
  • Domain specification (Useful if you have mirror sites/subdomains perhaps).

As you’ll probably notice, the author of this module references the use of Nodewords instead. If you really JUST want canonical URLs then this is a good option. Otherwise I agree, it would be better to go with Nodewords since the features (as far as canonical URLs) are the same.

Nodewords Module

The Nodewords module has a canonical url feature among other things (which would be really useful for other SEO tweaks you should be doing already if you’re not).

Nodewords has versions available for Drupal 5 and 6. Though the Drupal 5 version is no longer in development.

What was confusing to me with this module is they put the config for it under the URL path /admin/content/nodewords but show it under the Admin >> Content management >> Meta Tags area. I would expect to see “Nodewords”.

Nodewords configuration:

  • By default the canonical URL checkboxs are off. Check this on in two spots, “show on edit forms” and “output in HTML”.
  • Automatic placement is disabled as well. You can switch it on by using the radio button “Generate meta tag content when the meta tag content is empty” unless you like manual insertion.
  • Even if its on automated you can edit the fields it provides and it will stick if you have canonical URLs turned on under “show on edit form”.


I use the template.php code because of the level of control. Partly because most of the Drupal sites I work with are their own custom three headed monster. If you’re using a pretty vanilla install, you’d be fine to choose any of the modules.

Respond: Leave A Comment | Trackback URL

Entrupeners, Subscribe for the lastest tools, tips, and tutorials.

Leave a Reply

Custom Theme by Rob Malon | Content & Design © 2010 - RobMalon.Com - Chicago, Illinois