0

I have a requirement where my URL should be in format : http://domain/de-DE/pagename I have following settings done for my site:

<setting name="Languages.AlwaysStripLanguage">
    <patch:attribute name="value">true</patch:attribute>

For Link Manger Provider i have following properties set:

languageEmbedding="always"
addAspxExtension="false" 
alwaysIncludeServerUrl="false"
encodeNames="true" 
languageEmbedding="always"
languageLocation="filePath" 
lowercaseUrls="true" 
shortenUrls="false" 
useDisplayName="false"

Now the issue is, when I am giving internal link in general link for a page say, I am on page http://domain/de-DE/page1 and I have a navigation on which general link field has link of page2.

The expected url is http://domain/de-DE/page2 on navigation link

But I am getting URL as http://domain/de/DE/page2 which is redirecting me to 404 page.

I am fetching the link from general link field using method shown below:

 public static string LinkFieldurl(this Item item, ID fieldID)
    {
        if (item == null)
        {
            return string.Empty;
        }
        if (ID.IsNullOrEmpty(fieldID))
        {
            throw new ArgumentNullException(nameof(fieldID));
        }
        var field = item.Fields[fieldID];
        if (field == null)
        {
            return string.Empty;
        }
        var linkUrl = new LinkUrl();
        return linkUrl.GetUrl(item, fieldID.ToString());
    }

Now this is returning me URL with wrong format of language embedded in URL.

Please suggest me some way in which i can get correct format of language(de-DE) or if I am going somewhere wrong?

1 Answer 1

3

For starters, you should use the Sitecore.Data.Fields.LinkField class and GetFriendlyUrl(). To rewrite what you have currently:

public static string LinkFieldurl(this Item item, ID fieldID)
{
    if (item == null)
    {
        return string.Empty;
    }
    if (ID.IsNullOrEmpty(fieldID))
    {
        throw new ArgumentNullException(nameof(fieldID));
    }
    LinkField field = item.Fields[fieldID];
    return field?.GetFriendlyUrl() ?? string.Empty;
}

By casting your field to a LinkField, you should be able to retrieve the resulting URL pretty easily. You'll want to use GetFriendlyUrl() as it determines what the target type is (internal, external, media, etc.) and computes the URL accordingly.

You'll always want to set Languages.AlwaysStripLanguage to false. By setting this to true, you're telling Sitecore that you are not using language embedding in the URL and if Sitecore detects a language in the URL to just ignore it and treat it as part of the path. By setting it to true, you are telling Sitecore, yes, there is a language identifier in that path and to strip it out before trying to resolve the path of the item. This is likely your problem with the 404 issue - you've basically told Sitecore that de-DE is part of the path to resolve instead of the language embedding.

1
  • Hi chang , thanks for valuable inputs....it helped me.... although I have kept language.AlwaysStripLanguage as true as without it my pages are going to 404, I am still digging and searching for why it's working with true value....it's quiet strange behaving in my sitecore instance....But your inputs gave some idea... Commented Feb 28, 2018 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.