4
votes

we created multilingual website in FR, ES, EN from old classic asp website( which was also in 3 languages). In Old site by default in English language "en" was not embedding in the URL. but in our new sitecore website i have language Embedding="Always".

So when we redirect from old website to new website for 301 redirects in Google, if i opened my new website in French language and redirect from old site is in English ( which means that in the url there is no language i.e "en") link will point to french version).

So how could i add "en" to the url .

  HttpContext.Current.Request.RawUrl

i can use above to get the raw url but i am not getting how to display "en" in the browser url.

old website links:

www.abc.com/xyz.asp,
www.abc.com/fr/xyz.asp,
www.abc.com/es/xyz.asp

new website links:

www.abc.com/en/xyz.aspx,
www.abc.com/fr/xyz.aspx,
www.abc.com/es/xyz.aspx

thanks

5
There's is no way to achieve what you want out of the box, the only thing you would be able to do is to check that the request URL contains the language, if it doesn't then 302 redirect back to itself with the language included (but you've now had 3 hops to get to the content).jammykam
can you suggest any custom criteria to built thisSam
You could use the IIS URL Rewriting feature for this. Then define a pattern that redirects all your requests (something like /*.asp) to the new URL's, like /en/*.aspx. That only works if the paths are exactly the same and all the old paths exist in the new site.Ruud van Falier
Given the extra info, Ruud's suggestion of IIS URL Rewrite module is the easiest way to achieve thisjammykam
You could try this: stackoverflow.com/a/21136149/661447 if you don't want to use IIS Rewritejammykam

5 Answers

3
votes

How did you handle the language in the old website? How did you know a user was requesting a page in EN/FR/ES?

Since you are redirecting with a 301 from your old site you need to handle the language embedding into the URL from your old site. Handling it from the Sitecore side will add best only give you a "prettier" URL, with the added downside of having to add in another 302 redirect to the same page after you have inspected with the redirecting has the language embedded or not. Ruud's suggestion to use IIS Rewrite is a good one.

So when we redirect from old website to new website for 301 redirects in Google, if i opened my new website in French language and redirect from old site is in English ( which means that in the url there is no language i.e "en") link will point to french version).

Yes, this is how Sitecore works, it uses cookies to persist the last selected language, but context language is set in the following order. Your initial visit would set the language cookie.

  1. The sc_lang query string parameter.
  2. The language prefix in the path in the requested URL.
  3. The language cookie associated with the context site.
  4. The default language associated with the context logical site.
  5. The DefaultLanguage setting specified in web.config.

Overriding Sitecore’s Logic to Determine the Context Language

If you really don't want the language persisted between browser sessions then override the Sitecore.Pipelines.HttpRequest.LanguageResolver pipeline with your own logic to either NOT set the cookie (in which case you are totally relying on the request URL) OR set a cookie that will expire when the browser is closed. You can find an example from this blog post as well as an example in the previously linked John West blog post.

If your only concern is Google indexing, then I would probably not make any changes except add in a canonical link tags on your pages with the full URL including language (this will obviously be different for each language version).

<link rel="canonical" href="http://www.abc.com/en/xyz.aspx"/>
1
votes

I don't know exactly what you're asking here, but if you need the URL with language embedding for a specific Sitecore item, you just need configure languageEmbedding="always" for the link provider and then request the item's URL with the LinkManager:

Sitecore.Links.LinkManager.GetItemUrl(item);

If you need the URL for a specific language or explicitly set the languageEmbedding option in your code, you can set that in the UrlOptions:

var options = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
options.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Always;
options.EmbedLanguage(Sitecore.Globalization.Language.Parse("en"));

Sitecore.Links.LinkManager.GetItemUrl(item, options);

Hope that helps!

0
votes

Take a look at Sitecore.Pipelines.PreprocessRequest.StripLanguage... I'd overwrite this one, if no language is found using the existing function, I'd default back to en.

0
votes

You can use URLOptions for this please see below code -

protected UrlOptions URLOptions
        {
            get
            {
                if (urlOptions == null)
                {
                    urlOptions = new UrlOptions()
                    {
                        LanguageEmbedding = LanguageEmbedding.Always,
                        AddAspxExtension = false
                    };
                }
                return urlOptions;

            }
        }

Please pass this URloption object in geturl() function it will work.

LinkManager.GetItemUrl(itm, URLOptions)

0
votes

You can achieve this functionality by a sitecore setting.

enter image description here

Please refer above screenshot. Change languageEmbedding property to always from asNeeded This setting is done in Sitecore.config file. Please let me know if this helps you fulfill your requirement.