Development

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.

Self-referencing canonical tags

  1. Create a Velocity Format containing the following:

    <link rel="canonical" href="${currentPage.link}" />
  2. Create a system-region within the <head> tags of your Template(s). For example:

    <system-region name="CANONICAL" />
  3. Attach the Format to the CANONICAL system region (at the Template, Configuration, or Page level).
  4. Visit Manage Site > Site Settings and change the Link Rewriting setting to Absolute.
  5. Publish your page(s).
Tip: If you want to implement self-referencing canonical tags for an individual page only (or for just a few pages), you can attach this Format to the region at the page level. You can then set the page's link rewriting behavior to Absolute under Edit > Configure > Override the current Site's asset link rewriting behavior for this asset.

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.

  1. Create a system-region within the <head> tags of your Template(s). For example:

    <system-region name="CANONICAL" />
  2. 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>
  3. Add this Shared Field Group to the top of one or more of your existing Data Definitions.
  4. 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" />
    
  5. Attach this Format to the CANONICAL system region (at the Template, Configuration, or Page level).
  6. Visit Manage Site > Site Settings and change your Link Rewriting setting to Absolute.
  7. 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.