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; }
}
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.
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 :)
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.
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.
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
I looked up into my urlrewriting.config file
Thank you for your helps and responses.
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.
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
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:
Of course you will need to adjust this to whatever you wish to accomplish.
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 :-
The structure of my website is like that
-Content
Any clue why this is happening. Thank you in advance for all your collaboration and help
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
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
Thank you once again :) have a nice weekend
is working on a reply...