How can I set up canonical tags?
This article provides a couple of sample methods for configuring canonical tags in your Site(s). These are examples only and may require additional development/configuration depending on your organization's needs.
Add option for alternative canonical URL
The example below illustrates how you can set up additional options for pages to output a self-referencing canonical tag or point to another page as the canonical page.
- Create a system-region within the
<head>
tags of your Template(s). For example:
<system-region name="CANONICAL" />
-
Create a Shared Field Group with the following XML:
<system-data-structure>
<group identifier="canonical" label="Canonical Settings" restrict-to-groups="Administrators">
<text field-id="eff3e93cc0a8003d49767a63e99b3d5d" help-text="Enable this if the current page is NOT the canonical page, e.g. it's a duplicate." identifier="canonical-alternate" label="Specify alternate canonical URL?" required="true" type="checkbox">
<checkbox-item label="Yes" show-fields="canonical/canonical-url-type" value="yes"/>
</text>
<text field-id="eff3e93cc0a8003d49767a63e89422bd" identifier="canonical-url-type" label="Canonical URL Type" required="true" type="radiobutton">
<radio-item label="Choose Page" show-fields="canonical/page" value="page"/>
<radio-item label="Enter URL" show-fields="canonical/url" value="url"/>
</text>
<asset field-id="eff3e93cc0a8003d49767a63755ff381" help-text="Select the canonical page." identifier="page" label="Choose Page" type="page"/>
<text default="https://" field-id="eff3e93cc0a8003d49767a638b0b971b" help-text="Enter the URL for the canonical page." identifier="url" label="Enter URL"/>
</group>
</system-data-structure></system-data-structure> - Add this Shared Field Group to the top of one or more of your existing Data Definitions.
-
Create a Velocity Format containing the following:
#set ($canonical = $currentPage.getStructuredDataNode("canonical")) #if (!$canonical.getChild("canonical-alternate").hasTextValue("yes")) ## use current page's URL #set ($href = $currentPage.link) #else ## use alternate canonical page URL #if ($canonical.getChild("canonical-url-type").hasTextValue("page")) #set ($href = $canonical.getChild("page").asset.link) #elseif ($canonical.getChild("canonical-url-type").hasTextValue("url")) #set ($href = $canonical.getChild("url").textValue) #end #end ## output the canonical tag <link href="${href}" rel="canonical" />
- Attach this Format to the
CANONICAL
system region (at the Template, Configuration, or Page level). - Visit Manage Site > Site Settings and change your Link Rewriting setting to Absolute.
- Edit one or more pages, configure the additional canonical tag options, and publish the page(s).
Note: When editing a page with this setup, only users in the
Administrators
group will see these additional options. This is due to the restrict-to-groups="Administrators"
attribute on the field group. You can remove this restriction or adjust it to include the names of the appropriate groups in your Cascade CMS environment.