Auto Insert Canonical Tags Into Web Pages for SEO

I came across this Google Analytics Code Inserter Perl Script, which automatically insert Google Analytics code into your web pages that do not have the analytics tag in the first place. For SEO purposes, I amended the code to facilitate the auto insertion of canonical tags.

Upon executed on your server, the Perl script searches for canonical tag within the <head></head> tags. If the canonical tag exists, the script will not insert the tag to avoid duplication. If the web page doesn’t have the canonical tag, the script automatically inserts the tag before the </head> tag.

Simply execute the following command on your server to automatically insert canonical tags on all your web pages with .html file extension:

find . -name “*.html” -print0 | xargs -0 perl insert-tag.pl

For other file extensions, you can change to your desired file extension and execute the command:

For .php file extension: find . -name “*.php” -print0 | xargs -0 perl insert-tag.pl
For .asp file extension: find . -name “*.asp” -print0 | xargs -0 perl insert-tag.pl

Execute script only once
Execute the script at the root folder of your server, and all your web pages including those in the sub-folders will be updated accordingly. The executed script will extract the file name of your web pages, and insert the canonical tag into the corresponding web pages.

Auto backup your web pages
The script duplicates all your the original web page with a .bak file extension in case any of your web pages got messed up. Execute the following command to remove all these backup files:

find . -name “*.bak” -print0 | xargs -0 rm -f

The script is extremely handy,

  • when your website is not running on any Content Management System (CMS), i.e. WordPress that has plugin supports allowing auto-insertion of canonical tags dynamically onto all pages;
  • or when you are maintaining large amount of static pages. There are times that your web developer may forget to insert the canonical tag into the web page upon creating a new web page.

Download the Perl Script for your own usage. Edit the script and update your domain name in the variable — $prefix, before executing the script.

my $prefix = ‘<link rel=”canonical” href=”http://www.example.com/‘;

Cheok Lup is a data driven & hands-on practitioner in digital marketing, who has acquired more than 10 years of expertise in web development, web analytics, enterprise level SEO, paid search, and performance marketing strategies & tactics. He has worked with established global firms – GlobalSources.com, Accenture Interactive, SAP, educational institutions – Singapore Management University (SMU), small-medium businesses (SMBs/SMEs), and start-ups.