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

Incorrect URL's on host without language specified #194

Open
Eric-4NG opened this issue Apr 17, 2024 · 1 comment
Open

Incorrect URL's on host without language specified #194

Eric-4NG opened this issue Apr 17, 2024 · 1 comment

Comments

@Eric-4NG
Copy link

Hi there,

Currently I'm looking into an issue but I do not get any progress on it. We are having a site where by the hostname does not specify a culture.
So there is a homepage in the NL language. And a subpage in the NL language.

When clicking through edit mode in the CMS this subpage will be shown as below after navigating using "View on Website":

[HOST]/nl/dit-is-een-nederlandse-pagina

When generating the sitemap the url will be included as:

[HOST]/dit-is-een-nederlandse-pagina

So somehow the "nl/" part is lost, per debugging / viewing source in this repository I did not find any reason for this.
Anyone else experienced this before?

https://github.com/Geta/SEO.Sitemaps/blob/master/README.md#enabling-multi-language-support
Following those steps doesn't help either.

Used packages:
Geta.SEO.Sitemaps 4.0.1
Geta.SEO.Sitemaps.Commerce 4.0.1
EPiServer.CMS.Core 11.20.14
EPiServer.CMS.UI.Core 11.37.1
EPiServer.Commerce.Core 13.32.2

image

@Eric-4NG
Copy link
Author

Work around using code. Not really nice, but it seems to work for this case.
If anyone is aware of a better solution, feel free to reply 😉

[ModuleDependency(typeof(SitemapInitialization))]
public class CustomSitemapInitialization : IConfigurableModule
{
    public void Initialize(InitializationEngine context)
    {
        //Intentionally left empty.
    }

    public void Uninitialize(InitializationEngine context)
    {
        //Intentionally left empty.
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.Services.RemoveAll<IStandardSitemapXmlGenerator>();
        context.Services.AddTransient<IStandardSitemapXmlGenerator, CustomSitemapXmlGenerator>();
    }
}

public class CustomSitemapXmlGenerator : SitemapXmlGenerator, IStandardSitemapXmlGenerator
{
    private const string LanguageCode = "nl";

    public CustomSitemapXmlGenerator(ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, ISiteDefinitionRepository siteDefinitionRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter) : base(sitemapRepository, contentRepository, urlResolver, siteDefinitionRepository, languageBranchRepository, contentFilter)
    {
        //Intentionally left empty.
    }

    protected override XElement GenerateSiteElement(IContent contentData, string url)
    {
        var baseElement = base.GenerateSiteElement(contentData, url);

        //We are having issues with the /nl part for business.domain.nl. So now we will try to fix that.
        if (baseElement != null
            && IsBusinessUrl(url)
            && contentData is ILocalizable localizedContent
            && LanguageCode.Equals(localizedContent.Language.Name, StringComparison.OrdinalIgnoreCase))
        {
            var locationNode = baseElement
                .Nodes()
                .Where(x => x.NodeType == XmlNodeType.Element)
                .Cast<XElement>()
                .FirstOrDefault(x => x.Name == base.SitemapXmlNamespace + "loc");

            if (locationNode != null
                && Uri.TryCreate(locationNode.Value, UriKind.Absolute, out Uri uriResult)
                && !uriResult.LocalPath.StartsWith(LanguageCode, StringComparison.OrdinalIgnoreCase))
            {
                var builder = new UriBuilder(uriResult);
                builder.Path = $"/{LanguageCode}{builder.Path}";

                locationNode.Value = builder.ToString();
            }
        }

        return baseElement;
    }

    private bool IsBusinessUrl(string url)
    {
        if (string.IsNullOrEmpty(url))
            return false;

        return new Regex("^https?://(business:52426|(int-|prep-)?business.domain.nl)/.*$").IsMatch(url);
    }
}

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