Adding hreflang to head section. How to get translated url

44 Views Asked by At

I'm trying to update my NopCommerce 4.20 webshop and implement SEO suggestions. One suggestion is to add hreflang links to the header of each page, pointing to the different translated pages.

I implemented the following code in _Root.Head.cshtml:

    // Languages for hreflang tags:
    var allLanguages = languageService.GetAllLanguages();
    var segments = webHelper.GetThisPageUrl(false).Split('/');
    var pageUrl = "";
    for (var i = 4; i < segments.Length; i++)
    {
        pageUrl = $"{pageUrl}/{segments[i]}";
    }

    <!-- Show languages -->
    @foreach (var language in allLanguages)
    {
        var hreflang = languageService.GetTwoLetterIsoLanguageName(language).ToLower();
        var url = $"{webHelper.GetStoreLocation()}{hreflang}/{pageUrl}";
        <link rel="alternate" hreflang="@hreflang" href="@url" />
    }

This results in:

 <link rel="alternate" hreflang="en" href="https://www.josephiena.nl/en/foto-draak-in-de-lucht-digif-006">
 <link rel="alternate" hreflang="nl" href="https://www.josephiena.nl/nl/foto-draak-in-de-lucht-digif-006">

Although the EN link is working, the actual URL should be https://www.josephiena.nl/en/photo-dragon-in-the-sky-digif-006, because I also translated the title and thus the 'Search engine friendly page name' is also translated.

How do I get the correct translated URL?

0

There are 0 best solutions below