Setting Up Site-Wide Canonical URLs

Canonical URLs help search engines know what the correct page URL is to show in search results.  This is especially helpful when you have multiple URLs with basically the same content.  Use canonical URLs rather then getting penalized for duplicate content.

Additionally, Google has started to prefer https:// URLs over http:// URLs.  Most sites on Webvanta have not moved to https protocol.  If Google links to the https:// version of your website, but there is no security certificate, many browsers will show users a security error message that may deter users from visiting your website.

Read more about canonical URLs here: https://support.google.com/webmasters/answer/139066?hl=en#1

The following code addresses the security issue we have seen recently on our websites.

One note about using this site-wide setup for canonical URLs is that the system has the ability to find URLs with the same page slug but a different folder/URL path.  If you're site map, Custom Item Type default path structure or any other page links are not the actual page nesting structure, the indexed urls will change to match the actual page nesting structure.  If this is a concern, you can add canoncial urls page by page (which is what you should do if you do actually have multiple pages with basically the same content). 

The canonical URL link looks like this: <link rel="canonical" href="FULL-URL-HERE" />

 


 

Place the following code in a snippet that you can use on a template-by-template basis.  We usually place this code inside the html_head snippet that we already use in the page templates.  This code will create a canonical URL link for each page on your website where the code is found.  You likely will need to modify this code slightly depending on your page structure and site-specific needs.

 

<!-- First grab site and page specific information. w:account key='domain' grabs the published URL of your website; w:url grabs the full path of the current page; w:slug grabs only the page-specific slug.  Typically we don't use the /system/ url in our path structure so we universally remove it from any path.  You may wish to add more path removals. -->

<w:var name="live_domain">http://<w:account key='domain' /></w:var>
<w:var name="live_url"><w:url /></w:var>
<w:var name="live_url"><w:var name="live_url" format="gsub('/system/', '/')" /></w:var>
<w:var name="live_slug"><w:slug  /></w:var>

<!-- If the page is a category or taxonomy page, we grab the unique variables associated with the page -->
<w:taxonomy>
  <w:var name="category_permalink"><w:permalink /></w:var>
  <w:var name="category_id"><w:id /></w:var>
  <w:var name="category_name"><w:name format="gsub(' ', '%20')" /></w:var>
</w:taxonomy>

<!-- We now grab item-type or variable-type page specific data and perform the logic to determine the full url of the current page -->
<w:kb:item>
<!-- for item type pages -->
  <w:var name="item_path"><w:path url='default' /></w:var>

<!-- If it's a taxonomy/category or variable page type the system will "fill in" the attach_url variable.  If the page type isn't a category or variable page type, the attach_url variable will remain blank -->
  <w:var name="attach_url">
    <w:unless condition="{{category_permalink}}=''"><w:var name="live_url" />/<w:var name="category_permalink" /></w:unless>
    <w:unless condition="{{path-var-1}}=''">/<w:var name="path-var-1" /></w:unless>
    <w:unless condition="{{path-var-2}}=''">/<w:var name="path-var-2" /></w:unless>
    <w:unless condition="{{path-var-3}}=''">/<w:var name="path-var-3" /></w:unless>
  </w:var>

<!-- Based on the above variables we create the canonical URL for item type, category and variable type pages -->
  <w:var name="this_canonical_url">
    <w:unless condition="{{item_path}}=''"><w:var name='live_domain' /><w:var name="item_path" /></w:unless>
    <w:if condition="{{item_path}}=''">
      <w:unless condition="{{attach_url}}=''"><w:var name='live_domain' /><w:var name="attach_url" /></w:unless>
      <w:if condition="{{attach_url}}=''"><w:var name='live_domain' /><w:var name="live_url" /></w:if>
    </w:if>
  </w:var>

 

<!-- Here we rewrite the canonical URL for very specific pages.  You can setup as many of these overwrites as needed -->

<!-- This code is for the older list page type -->

  <w:if condition="{{live_slug}}='tag-search-results'">
    <w:var name="this_canonical_url">
      <w:var name='live_domain' /><w:var name="live_url" />/<w:var name='category_id'/>-<w:var name='category_name'/>/<w:kb:resourcetype:id/>
    </w:var>
  </w:if>

<!-- The system shows the slug /search rather than /search_result to the public so we need to customize this-->

  <w:if condition="{{live_slug}}='search_results'">
    <w:var name="this_canonical_url"><w:var name='live_domain' />/search</w:var>
  </w:if>

<!-- Lastly, we write the canonical url -->
  <link rel="canonical" href="<w:var name='this_canonical_url' />" />
</w:kb:item>