Skip to content

Commit

Permalink
DNN-21481 Add alternate link w/ "hreflang" to sitemap.aspx for multil…
Browse files Browse the repository at this point in the history
…ingual websites
  • Loading branch information
vmasanas committed Nov 30, 2013
1 parent 68e74bc commit d470a5f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
49 changes: 47 additions & 2 deletions DNN Platform/Library/Services/Sitemap/CoreSitemapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,51 @@ private SitemapUrl GetPageUrl(TabInfo objTab, string language)
}
pageUrl.ChangeFrequency = SitemapChangeFrequency.Daily;

// support for alternate pages: https://support.google.com/webmasters/answer/2620865?hl=en
if (ps.ContentLocalizationEnabled && !objTab.IsNeutralCulture)
{
List<AlternateUrl> alternates = new List<AlternateUrl>();
TabInfo currentTab = objTab;

if (!objTab.IsDefaultLanguage)
currentTab = objTab.DefaultLanguageTab;

foreach (TabInfo localized in currentTab.LocalizedTabs.Values)
{
if ((!localized.IsDeleted && !localized.DisableLink && localized.TabType == TabType.Normal) &&
(Null.IsNull(localized.StartDate) || localized.StartDate < DateTime.Now) &&
(Null.IsNull(localized.EndDate) || localized.EndDate > DateTime.Now) &&
(IsTabPublic(localized.TabPermissions)) &&
(includeHiddenPages || localized.IsVisible))
{
string alternateUrl = Globals.NavigateURL(localized.TabID, localized.IsSuperTab, ps, "", localized.CultureCode);

if (alternateUrl.ToLower().IndexOf(portalAlias.ToLower(), StringComparison.Ordinal) == -1)
{
// code to fix a bug in dnn5.1.2 for navigateurl
if ((HttpContext.Current != null))
{
alternateUrl = Globals.AddHTTP(HttpContext.Current.Request.Url.Host + alternateUrl);
}
else
{
// try to use the portalalias
alternateUrl = Globals.AddHTTP(portalAlias.ToLower()) + alternateUrl;
}
}
alternates.Add(new AlternateUrl() { Url = alternateUrl, Language = localized.CultureCode });
}
}

if (alternates.Count > 0)
{
// add self to the list
alternates.Add(new AlternateUrl() { Url = pageUrl.Url, Language = objTab.CultureCode });

pageUrl.AlternateUrls = alternates;
}
}

return pageUrl;
}

Expand All @@ -150,7 +195,7 @@ protected float GetPriority(TabInfo objTab)
}
else
{
priority = Convert.ToSingle(1 - (objTab.Level*0.1));
priority = Convert.ToSingle(1 - (objTab.Level * 0.1));
}

if (priority < minPagePriority)
Expand All @@ -173,7 +218,7 @@ public virtual bool IsTabPublic(TabPermissionCollection objTabPermissions)
if ((roles != null))
{
// permissions strings are encoded with Deny permissions at the beginning and Grant permissions at the end for optimal performance
foreach (string role in roles.Split(new[] {';'}))
foreach (string role in roles.Split(new[] { ';' }))
{
if (!string.IsNullOrEmpty(role))
{
Expand Down
13 changes: 13 additions & 0 deletions DNN Platform/Library/Services/Sitemap/SitemapBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ private void WriteSitemap(bool cached, TextWriter output, int index, List<Sitema
// build header
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/" + SITEMAP_VERSION);
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "xhtml", null, "http://www.w3.org/1999/xhtml");
var schemaLocation = "http://www.sitemaps.org/schemas/sitemap/" + SITEMAP_VERSION;
writer.WriteAttributeString("xsi", "schemaLocation", null, string.Format("{0} {0}/sitemap.xsd", schemaLocation));

Expand Down Expand Up @@ -300,6 +301,18 @@ private void AddURL(SitemapUrl sitemapUrl)
writer.WriteElementString("lastmod", sitemapUrl.LastModified.ToString("yyyy-MM-dd"));
writer.WriteElementString("changefreq", sitemapUrl.ChangeFrequency.ToString().ToLower());
writer.WriteElementString("priority", sitemapUrl.Priority.ToString("F01", CultureInfo.InvariantCulture));

if (sitemapUrl.AlternateUrls != null)
{
foreach (AlternateUrl alternate in sitemapUrl.AlternateUrls)
{
writer.WriteStartElement("link", "http://www.w3.org/1999/xhtml");
writer.WriteAttributeString("rel", "alternate");
writer.WriteAttributeString("hreflang", alternate.Language);
writer.WriteAttributeString("href", alternate.Url);
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}

Expand Down
13 changes: 10 additions & 3 deletions DNN Platform/Library/Services/Sitemap/SitemapUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#region Usings

using System;
using System.Collections.Generic;

#endregion

Expand All @@ -30,13 +31,19 @@ public class SitemapUrl
{
public string Url { get; set; }


public DateTime LastModified { get; set; }


public SitemapChangeFrequency ChangeFrequency { get; set; }


public float Priority { get; set; }

public List<AlternateUrl> AlternateUrls { get; set; }
}

public class AlternateUrl
{
public string Language { get; set; }

public string Url { get; set; }
}
}

0 comments on commit d470a5f

Please sign in to comment.