I had previously implemented canonical tags on my website using the following code:
<link rel="canonical" <%=EPiServer.Web.UrlRewriteProvider.UrlPreventRewriteAttribute %>="true" href="<%= CurrentPage["Canonical"] %>" />
When I enter a URL into the canonical page property, it displays in page source - great. However, what I would prefer is to have the current page absolute URL displayed if the canonical page property is null. This is where I get a little stuck, as I am not sure how best to acomplish this.
looking through the SDK, I found this code, which checks to see if a property is null - but am not sure how I would get it to write the absolute URL if the property is blank:
PropertyData property = CurrentPage.Property["PropertyName"];
if (property != null && !property.IsNull)
Any help would be greatly appreciated!
Maybe I am oversimplifying things, but would this work:
<link rel="canonical" <%=EPiServer.Web.UrlRewriteProvider.UrlPreventRewriteAttribute %>="true" href="<%= CurrentPage["Canonical"] ?? CurrentPage.PageLink %>" />
I tried the code and it word (sort of). CurrentPage.PageLink returns the internal relative link (/Templates/Pages/x) rather than an absolute external URL such as http://www.xyz.com/category/page.
I recently implemented some code which will return the absolute external URL, so I will use this in place of the CurrentPage.PageLink and it will work perfectly.
Sometimes the most simple answers are the best, this almost does exactly that I wanted - I just need to do a little tweaking.
Thanks for your help!