Canonical Links with Full URLs

Scott Widmer asked on June 8, 2016 20:24

I'm using this tutorial to add canonical links to my pages. I have the link tag being added to the HEAD area of my documents but only the filename/path. I need the full URL but don't know what Kentico variable to access to output the full URL instead of the relative to root path. (i.e. instead of

Also, I'm wondering how to add the canonical links to an aspx template page as well. Currently I'm getting an error (The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.) when I use the following code:

<%@ Register Src="~/CMSWebParts/General/headhtml.ascx" TagName="HeadHTMLCode" TagPrefix="uc1" %>
<uc1:HeadHTMLCode ID="zoneTopInfo" runat="server" Content="{%GetCanonicalLink%}" />

Recent Answers


Roman Hutnyk answered on June 8, 2016 21:08

Could you please complete your question and provide as much details as possible? Thanks!

0 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 8, 2016 21:11 (last edited on June 8, 2016 21:13)

I entered more text than is showing up. I've tried editing multiple time and still the same single line appears.

Wasn't sure if it was just my browser not displaying properly. Complete now.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 8, 2016 21:38

The problem is you're using a Macro in an ASCX transformation. Your best bet it to create your custom macro as a global function first, then us it in you custom macro method, then create a custom transformation method you can call it from. This way the single function will be available all over for you to use vs. just in the macro.

Now that you've created that code, update your transformation to be something like

Content="<%# GetCanonicalLink() %>"

Just an FYI, that article is for v5 or older so you may want to be careful if your implementing it in a newer version.

1 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 8, 2016 21:53

Brenden,

Thanks! I'm actually working with 5.5 on one site and 8.1 one for my next implementation of this. I'm not an .Net developer and kind of fumbling my way through this.

I'll take a look at your suggestion and see if it gets me any further.

How would I get the full URL instead of the relative to root path for my canonical link?

Thanks, Scott

0 votesVote for this answer Mark as a Correct answer

Pedro Costa answered on June 8, 2016 23:56

Think it's the same on 5.5 here's the 8.2 version https://devnet.kentico.com/docs/8_2/api/html/M_CMS_Helpers_URLHelper_GetAbsoluteUrl.htm

The URLHelper might be on another namespace on 5.5 tho.

Cheers, P.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 9, 2016 02:57

Scott, you can use this to get the current document's absolute url

CMS.DocumentEngine.DocumentContext.CurrentDocument.AbsoluteURL
1 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 9, 2016 14:23

Brenden,

Thanks again. Maybe I'm confused on where I would use that since I asked two questions.

I added your suggestion to the macro and get the following error:

Compiler Error Message: CS0234: The type or namespace name 'DocumentEngine' does not exist in the namespace 'CMS' (are you missing an assembly reference?)

Source Error:

Line 51: PageInfo currentPage = CMSContext.CurrentPageInfo;

Line 52: match = true;

Line 53: return "<link rel=\"canonical\" href=\"" + CMS.DocumentEngine.DocumentContext.CurrentDocument.AbsoluteURL + "\" />";

Line 54: }

Line 55:

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 9, 2016 15:14

Sorry about that Scott, I forgot you're working in 5.5 and not v8 or v9. I believe the code for v5 is

CMSContext.CurrentDocument.AbsoluteURL
0 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 9, 2016 16:02

Brenden,

Now I get this:

Compiler Error Message: CS1061: 'CMS.TreeEngine.TreeNode' does not contain a definition for 'AbsoluteURL' and no extension method 'AbsoluteURL' accepting a first argument of type 'CMS.TreeEngine.TreeNode' could be found (are you missing a using directive or an assembly reference?)

Is there somewhere on the Kentico site I can hunt for usage and reference these variables for myself within the API? I've looked many times and direct searches for these variables return zero results. I don't mind reading/learning when trying to figure this stuff out but pretty frustrating when every search turns up empty or anything I find/try results in an error :)

Thanks for the help!

-Scott

0 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 9, 2016 16:05

Here's my complete block of code for the macro at the moment which returned the above error.

/// Custom macro handler
/// 
///Sender (active macro resolver)
///Expression to resolve
///Returns true if the macro matches (was resolved)
public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{   
  match = false;
  string result = expression;

  // Add your custom macro evaluation
  switch (expression.ToLower())
  {     
    case "getcanonicallink":
      PageInfo currentPage = CMSContext.CurrentPageInfo;
      match = true;
      return "<link rel=\"canonical\" href=\"" + CMSContext.CurrentDocument.AbsoluteURL + "\" />";
  }

  return result;
}
0 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 10, 2016 20:13

Jumping over to the 8.1 site, if I add this to the master template layout, via Kentico, not code files, I get the results I want. However, if I place this in the header block, the value isn't parsed and is included in the header as-is.

<link rel="canonical" href="<%= CMS.DocumentEngine.DocumentContext.CurrentDocument.AbsoluteURL %>"/>

Is there a way to get this into the header without touching the code files? I can't seem to figure out which master page is being used as I've updated every combination I could find on the server without error or output.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 14, 2016 13:40 (last edited on December 10, 2019 02:30)

In an 8.x site you can use the HEAD HTML Webpart and set the value to a macro:

<link rel="canonical" href="{% CurrentDocument.AbsoluteURL |(identity)GlobalAdministrator%}"/>
0 votesVote for this answer Mark as a Correct answer

Scott Widmer answered on June 14, 2016 15:05

Brenden,

Thanks, that is exactly what I did on the 8.1 site. So much easier than 5.5.

Still haven't found a way to get the absolute URL in 5.5.

-Scott

0 votesVote for this answer Mark as a Correct answer

Aaron Fickes answered on August 18, 2016 00:47 (last edited on December 10, 2019 02:30)

Resurrecting an old thread with an added twist. How can you handle this with IIS Rewrites?

A website currently has /topics/thinghere rewritten to /topics?id=thinghere so it can query data in a webpart on the page.

When trying to add a canonical url I've tried the following with no avail:

{% CurrentDocument.AbsoluteUrl |(identity)GlobalAdministrator%} resolves to http://domain.com/topics
HttpContext.Current.Request.AbsoluteUri resolves to http://domain.com/PortalTemplate...

Thoughts?

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.