2

I have multi-lingual website, which has both English and Arabic languages. In the LinkManager configuration languageEmbedding is set to always, you can see the full configuration below.

As it is set to always, in menu (or wherever links are generated from the code), links are embed with language. When user manually types the URL in browser, language code would not be present, it still resolves properly (by reading the language from cookie). But we also need to change the URL by embedding the language code.

<linkManager defaultProvider="sitecore">
        <providers>
            <clear />
            <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel"
                 addAspxExtension="false" 
                 alwaysIncludeServerUrl="false" 
                 encodeNames="true" 
                 languageEmbedding="always" 
                 languageLocation="filePath" 
                 lowercaseUrls="true" 
                 shortenUrls="false" 
                 useDisplayName="false" 
                 applyForSites="" 
                 ignoreForSites="shell,login,admin" 
                 forceFriendlyUrl="true" 
                 trailingSlash="false"
                 onlyApplyForSiteContent="true" />
        </providers>
    </linkManager>

Can we achieve it by any processor or any redirect rules?

6

You can add your own processor to the httpRequestBegin pipeline after the ItemResolver processor. Your processor should generate Sitecore.Content.Item URL and compare it with HttpContext.Current.Request.RawUrl. If new url contains language and RawUrl does not contain, just redirect user to the newly generated url.

Something like this should do the trick (I haven't tested it):

public class EnsureLanguage : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        if (Context.PageMode.IsNormal && Context.Item != null)
        {
            var correctUrl = LinkManager.GetItemUrl(Context.Item);

            if (!HttpContext.Current.Request.RawUrl.StartsWith(correctUrl))
            {
                // additionally add query string parameters
                HttpContext.Current.Response.Redirect(correctUrl);
            }
        }
    }
}
  • Thank you for your answer. Apologies for not being clear, it is actually not from the content side, but when website visitor types the URL in browser, we need to embed the language to the URL. updated the question. – Gopikrishna Gujjula Nov 14 '18 at 8:10
  • See edited answer – Marek Musielak Nov 14 '18 at 8:41
2

A custom httpRequestBegin processor is definitely a good approach to take, but I would check the Language property of the already resolved item against the requested URL, and redirect to the language prepended URL if required.

public class EnsureLanguage : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        if (Context.PageMode.IsNormal
            && Context.Item != null
            && !args.Context.Request.RawUrl.StartsWith($"/{Sitecore.Context.Item.Language.Name}"))
        {
            HttpContext.Current.Response.Redirect($"/{Sitecore.Context.Item.Language.Name}{args.Context.Request.RawUrl}");
        }
    }
}

This should perform better than trying to resolve the URL again for every single request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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