Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Preetee Gangoosirdar 56 posts 257 karma points
    Jun 22, 2015 @ 10:36
    Preetee Gangoosirdar
    0

    ERR_TOO_MANY_REDIRECTS on browser language detection

    Hi,

    Grateful if someone can help me on this issue . Am trying to put in a place a browser language detection on my website's homepage template only. But it having an redirect looping with the following code

    enter code here
    
    @using System;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.cms.businesslogic.web;
    
     @{
        var rootNode = Model.AncestorOrSelf(1);
        string lang = @rootNode.Name.ToString().ToLower();
    
        string[] languages = HttpContext.Current.Request.UserLanguages; 
        string userCustomLanguage = languages[0].Substring(0, 2);
            bool gotodefault=true;
    
         if (gotodefault && !string.IsNullOrEmpty(userCustomLanguage)){
            switch (userCustomLanguage){
                              case "zh":
                                       Response.RedirectPermanent("/cn");                                   
                                       break;
    
                             case "fr":
                                        Response.RedirectPermanent("/fr");
                                        break;
    
                            case "de":
                                       Response.RedirectPermanent("/de");
                                       break;
    
                            case "ru":                   
                                       Response.RedirectPermanent("/ru");                                   
                                       break;
    
                             default:
                                       Response.RedirectPermanent("/en");   
                                       break;
           }
        }  
       else{ gotodefault=false; }
     }
    

    I looked up into my urlrewriting.config file

    enter code here
    
     <add name="En-default"
            virtualUrl= "^~/en/default"
            rewriteUrlParameter="ExcludeFromClientQueryString"
             destinationUrl="~/"
             ignoreCase="true"
            redirect="Application"
            redirectMode="Permanent" />
    <add name="ru-default"
                virtualUrl= "^~/ru/default"
                rewriteUrlParameter="ExcludeFromClientQueryString"
                 destinationUrl="~/ru/"
                 ignoreCase="true"
                redirect="Application"
                redirectMode="Permanent" />
        <add name="cn-default"
                virtualUrl= "^~/cn/default"
                rewriteUrlParameter="ExcludeFromClientQueryString"
                 destinationUrl="~/cn/"
                 ignoreCase="true"
                redirect="Application"
                redirectMode="Permanent" />
      <add name="de-default"
                virtualUrl= "^~/de/default"
                rewriteUrlParameter="ExcludeFromClientQueryString"
                 destinationUrl="~/de/"
                 ignoreCase="true"
                redirect="Application"
                redirectMode="Permanent" />
    

    Thank you for your helps and responses.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 22, 2015 @ 11:57
    Dan Diplo
    0

    It looks to me like what is happening is (take for example the user language is "en"):

    You detect language Language is "en" You redirect to "/en" You detect language Language is "en" You redirect to "/en" (even though you are already there) You detect language Language is "en" You redirect to "/en" ... etc

    So when the user language matches the homepage you are on you don't want to perform any further redirect.

  • Preetee Gangoosirdar 56 posts 257 karma points
    Jun 22, 2015 @ 12:05
    Preetee Gangoosirdar
    0

    Hi Dan,

    Thank you for your response. Do you think i need to create a session variable to determine whether the user is already on the language, so as to get out of this loop.

    Any reference or solution to resolve this issue would be mostly appreciated. Thank you :)

    Best Regards Preetee

  • Doron Uziel 23 posts 93 karma points
    Jun 22, 2015 @ 15:39
    Doron Uziel
    0

    I think that in 99.9% of all cases in which you consider using Session variable, there is probably a better alternative.

    In your case you create an infinite loop of redirects as you do not check to see if you have already landed on the desired language url.

    What you might want to check is something along the line of:

    if(Request.Url.Segments.Length>0 && Request.Url.Segments[0].Trim("/").Equals(userCustomLanguage))
    {
            //You are already within the desired language context
          return;
    }
    

    Of course you will need to adjust this to whatever you wish to accomplish.

  • Preetee Gangoosirdar 56 posts 257 karma points
    Jun 25, 2015 @ 11:40
    Preetee Gangoosirdar
    0

    Hi Doron,

    Thanks for your kind return on this issue. However i did noticed that a simple redirection keep on doing an infinite loop on my homepage tempate.

    Supposed i want to simply redirect to the Chinese version of my website as follows :-

     <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %> <script runat="server">protected void Page_Load(object sender, EventArgs e){    Response.Redirect("/cn");}</script>
    

    The structure of my website is like that

    -Content

    • en
    • fr
    • cn
    • de
    • ru

    Any clue why this is happening. Thank you in advance for all your collaboration and help

  • Doron Uziel 23 posts 93 karma points
    Jun 25, 2015 @ 18:34
    Doron Uziel
    0

    Hi Preetee,

    Not so sure I understand what is going on and what is it that you are trying to achieve.

    It was my initial understanding that you were trying to redirect users to their native lanugage's (or more accurate: their browser language's) homepage, if they land on other language homepage.

    for example if somene whose browser language is set to French (fr) lands on the /ru homepage you would like to redirect him to /fr automatically.

    As Dan pointed out above, in your code, no matter what is the language the user lands on a redirect always happens, so you need to change your code flow so it would happen only when the current-node language is deifferent than the browser language.

    So what you should do is before your switch statement check if the browser language is different than the current node language. You already have the lang variable which is the current node language and the userCustomLanguage which is the browser language (though it seems that the are not the same for each langauge - for example zh!=cn).

    So it shouldn't be that difficult.

    Also - the gotodefault variable is completly useless, so drop it.

    And really, it is not a good practice to write code within the ~/umbraco/masterpages/default.master , and the code itself does not seem right.

    Doron

  • Preetee Gangoosirdar 56 posts 257 karma points
    Jun 26, 2015 @ 11:22
    Preetee Gangoosirdar
    100

    Hi Dan & Doron,

    Thanks you Dan & Doron for your kind feedback. I was able to resolve this issue following your help. Please find the code below

     @{
    var rootNode = Model.AncestorOrSelf(1);
    string lang = @rootNode.Name.ToString().ToLower();
    
    HttpRequest Request = HttpContext.Current.Request;  
    string[] languages = HttpContext.Current.Request.UserLanguages; 
    string userCustomLanguage = languages[0].Substring(0, 2);   
    
    var firstDomainLanguage = Domain.GetDomainsById(Model.AncestorOrSelf(1).Id)[0].Language;
    var domainlang= @firstDomainLanguage.CultureAlias.Substring(0, 2);
    
    @Html.Raw("Current language node "+ @lang+"<br/>");
    @Html.Raw("Browser user language  "+ @userCustomLanguage + "<br/>");
    
    @Html.Raw("Domain language  "+ @domainlang+"<br/>");
    
    if(lang==userCustomLanguage || (lang=="cn" && userCustomLanguage=="zh") ) {
        return;     }   
    else{
         switch (userCustomLanguage){
                          case "zh":
                                   Response.RedirectPermanent("/cn");                                   
                                   break;
    
                         case "fr":
                                    Response.RedirectPermanent("/fr");
                                    break;
    
                        case "de":
                                   Response.RedirectPermanent("/de");
                                   break;
    
                        case "ru":                   
                                   Response.RedirectPermanent("/ru");                                   
                                   break;
                         default:
                                   Response.RedirectPermanent("/en");   
                                   break;
       }    
        }   }
    

    Thank you once again :) have a nice weekend

Please Sign in or register to post replies

Write your reply to:

Draft