Try our conversational search powered by Generative AI!

Canonical tag default to absolute URL if page property is null

Vote:
 

Hi all,

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)
{
    DoSomething();
}

    

Any help would be greatly appreciated!

#61376
Sep 18, 2012 13:32
Vote:
 

Maybe I am oversimplifying things, but would this work:

<link rel="canonical" <%=EPiServer.Web.UrlRewriteProvider.UrlPreventRewriteAttribute %>="true" href="<%= CurrentPage["Canonical"] ??  CurrentPage.PageLink %>" />

#61381
Sep 18, 2012 14:17
Vote:
 

Hi Toni,

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!

#61386
Sep 18, 2012 15:03
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.