Created
October 27, 2017 09:35
-
-
Save ps-team/0cf7b4fca207a77dd7ab64d0e8f0fcfb to your computer and use it in GitHub Desktop.
Canonical tags in Razor. I made some changes to point URL's with query strings back at the URL without the QS as this was showing up as duplicated content in GA.
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