Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemap returns 404 - SiteDefinition resolving #149

Open
trouden opened this issue Apr 2, 2021 · 0 comments
Open

Sitemap returns 404 - SiteDefinition resolving #149

trouden opened this issue Apr 2, 2021 · 0 comments

Comments

@trouden
Copy link

trouden commented Apr 2, 2021

The SitemapRepository tries to resolve the SiteDefinition using the url hostname ( See code fragment below). However the HostDefinition Name property is mapped to the url authority. When using a hostname with a port number, e.g. 'http://my.site:21806' no site definition will be resolved and a 404 will be returned. This did function correctly in earlier versions ( 3.1.1 ).

.Net Framework 4.7.2
Episerver.CMS 11.20.5
Episerver.Commerce 13.30
Geta.SEO.Sitemaps 4.0.0
Geta.SEO.Sitemaps.Commerce 4.0.0

public SitemapData GetSitemapData(string requestUrl)
{
            var url = new Url(requestUrl); 
            
            // contains the sitemap URL, for example en/sitemap.xml
            var host = url.Path.TrimStart('/').ToLowerInvariant();

            var siteDefinition = _siteDefinitionResolver.GetByHostname(url.Host, true, out _);
            if (siteDefinition == null)
            {
                return null;
            }

            var sitemapData = GetAllSitemapData()?.Where(x =>
                GetHostWithLanguage(x) == host &&
                (x.SiteUrl == null || siteDefinition.Hosts.Any(h => h.Name == new Url(x.SiteUrl).Authority))).ToList();

            if (sitemapData?.Count == 1)
            {
                return sitemapData.FirstOrDefault();
            }

            // Could happen that we found multiple sitemaps when for each host in the SiteDefinition a Sitemap is created.
            // In that case, use the requestURL to get the correct SiteMapData
            return sitemapData?.FirstOrDefault(x => new Url(x.SiteUrl).Authority == url.Authority);
}
    public static HostDefinition Find(
      this IEnumerable<HostDefinition> hosts,
      string hostName,
      bool fallbackToWildcard)
    {
      bool flag = HostDefinition.IsWildcardHost(hostName);
      HostDefinition hostDefinition1 = (HostDefinition) null;
      foreach (HostDefinition hostDefinition2 in hosts.Where<HostDefinition>((Func<HostDefinition, bool>) (h => h.Authority != null && h.Authority.Matches(hostName, fallbackToWildcard))))
      {
        if (flag || !hostDefinition2.IsWildcardHost())
          return hostDefinition2;
        if (fallbackToWildcard)
          hostDefinition1 = hostDefinition2;
      }
      return hostDefinition1;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant