How can I set up canonical tags?
This article gives two sample methods for configuring canonical tags. They're examples, and may need additional development depending on your needs.
Self-referencing canonical tags
-
Create a Velocity Format containing:
Velocity<link rel="canonical" href="${currentPage.link}" /> -
Create a system-region within the
<head>tags of your Template:HTML<system-region name="CANONICAL" /> - Attach the Format to the
CANONICALregion, at the Template, Configuration, or Page level. - Visit Manage Site > Site Settings and change Link Rewriting to Absolute.
- Publish your pages.
For a single page or a few pages, attach the Format at the page level and set that page's link rewriting under Edit > Configure > Override the current Site's asset link rewriting behavior for this asset.
Adding an alternative canonical URL option
This example lets a page output a self-referencing canonical tag or point at another page instead.
-
Create a system-region within the
<head>tags of your Template:HTML<system-region name="CANONICAL" /> -
Create a Shared Field group with this XML:
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> - Add this Shared Field group to the top of one or more existing Data Definitions.
-
Create a Velocity Format containing:
Velocity#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
CANONICALregion. - Visit Manage Site > Site Settings and change Link Rewriting to Absolute.
- Edit one or more pages, configure the canonical options, and publish.
Only users in the Administrators group see these options, because of the restrict-to-groups="Administrators" attribute on the field group. Remove or adjust that attribute for your own groups.