Knowledge Base

Creating Canonical URLs with Handlebars

Last Modified:
28 Jan 2025
User Level:
Power User
Complexity:
Beginner Friendly
Canonical URLs tell search engines the preferred web page URLs to index, resolving duplicate content issues and improving SEO.

A Canonical URL is the preferred version of a web page that you want search engines to prioritize and display in search results. It helps address situations where the same content is accessible via multiple URLs (for example, when you have mirrored a section in Terminalfour to another area of your site).

Using a Canonical URL ensures that search engines understand which version of a page to index, avoiding duplicate content issues that could harm your site's search engine rankings. It also consolidates ranking signals like backlinks and helps deliver a more consistent experience for users and search engines alike.

Outputting a canonical url with Handlebars

With Handlebars, adding a canonical URL to your page layouts is straightforward.

If your Page Layout uses a Handlebars Processor, you can add the following code snippet between the opening and closing <head> tags of your layout:

<link rel="canonical" href="{{canonicalURL}}"/>

That’s it!

When Terminalfour publishes a page using this layout, it will automatically check if the current page is a mirror of another section. If it is, the link tag will include a reference to the original page.

If using a T4 Tag based Page Layout

If your Page Layout that’s using a T4 Tag Processor then you have two options:

Convert your Page Layout to use the Handlebars

You can change the Layout processor to handlebars and update all your T4 Tags to the Handlebars equivalents. Once that’s done, you can add the canonical URL helper as described above. Converting a Page Layout to Handlebars is a relatively quick task and will future proof your layouts for further improvements that could come in future Terminalfour releases.

Use the SectionMetaDescription approach

If you are not in a position to be able to update your Page Layouts to the Handlebars Processor then you can also take another approach to make use of the canonicalURL Helper.

The SectionMetaDescription content type (if enabled) will automatically add a hidden content item to every section of your site. You can modify this Content Type, and add a new Content Layout to it to output the Canonical URL.

  1. Edit the SectionMetaDescription Content Type
  2. Add a new content layout called text/canonical-url
  3. Ensure the Content Layout Processor is set to “Handlebars Content”
  4. Add the following code to the layout:

<link rel="canonical" href="{{canonicalURL}}"/>

Now we need to ensure that this gets added into our Page Layout. To do that we’ll use a Related Content navigation object.

  1. Go to Assets > Navigation
  2. Choose “Add new navigation”
  3. Select “Related Content”
    1. Set the name to “Return Canonical URL”
    2. Give it a Description and apply it to a group if required
    3. Change the “Content Layouts” option to “Use alternate content layout”
    4. Enter the value `text/canonical-url`
    5. Click “Next” to complete the creation of your Navigation object.
  4. Copy the T4 Tag for your newly created navigation object and paste it into your Page Layout between the opening and closing <head> tags. 
Back to top