Created
July 6, 2017 11:55
-
-
Save ps-team/d26e4de8ae98f86413750b7269cce8d7 to your computer and use it in GitHub Desktop.
Canonical tagging in Contensis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Contensis.Framework.Web | |
@using Contensis.Framework.Web.Search | |
@using System.Collections | |
@using System.Collections.ObjectModel | |
@using System.Text.RegularExpressions; | |
@{ | |
if (CurrentNode.Data.Property_SynchronisationSourceMasterContentID >= 0) | |
{ | |
int masterPageContentID = CurrentNode.Data.Property_SynchronisationSourceMasterContentID; | |
Node masterPage = null; | |
//If the master page content ID doesn't match the current page content ID then we are on a slave page | |
if (masterPageContentID != CurrentNode.Data.Property_C_ID) | |
{ | |
//Here we are returning the source page as a Node so we can use WebApi Methods | |
IQuery masterPageQuery = Query.Where("Property_C_ID").IsEqualTo(masterPageContentID.ToString()).And("Property_CT_ID").IsEqualTo("0"); | |
ReadOnlyCollection<ContentNode> masterPages = new NodeFinder().Find(masterPageQuery); | |
masterPage = masterPages[0]; | |
((CMS_API.WebUI.Page.BasePage)HttpContext.Current.CurrentHandler).ContensisHeader.Text += "<meta rel='canonical' href='" + @masterPage.Path + "' />"; | |
} | |
// If it's not a slave page, but the page has a query string, we need to set the canonical tag to point at the non-querystringed version. And yes, querystringed is a word. | |
else if (Request.QueryString.Count > 0 && CurrentNode.Data.Property_SynchronisationSourceMasterContentID >= 0) | |
{ | |
string currentUrl = CurrentNode.Path; | |
var canonicalUrl = currentUrl.Split('?')[0]; | |
((CMS_API.WebUI.Page.BasePage)HttpContext.Current.CurrentHandler).ContensisHeader.Text += "<meta rel='canonical' href='" + @canonicalUrl.ToString() + "' />"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment