I am trying to redirect the user to sub domain based on his IP address location. I have a page load observer which runs a function on each request and it gets the user Location and when I try to redirect to another domain it gives me "Too many redirects" error and I can't figure out a way to solve this issue.
Currently my code looks like as follows
string CountryName = "";
var Country = HttpContext.Current.Response.Cookies["Country"];
Country.Expires = DateTime.Now.AddDays(365);
var ip = HttpContext.Current.Request.UserHostAddress;
if (!string.IsNullOrEmpty(ip) && ip != null && ip != "127.0.0.1")
{
using (var client = new WebServiceClient(xxxxx, "xxxxxxxx"))
{
var IpCountry = client.Country(ip);
CountryName = IpCountry.Country.Name;
}
switch (CountryName)
{
case "Denmark":
if (Country.Value != CountryName)
{
Country.Value = CountryName;
HttpContext.Current.Response.Redirect("/");
}
break;
case "United Kingdom":
if (Country.Value != CountryName)
{
Country.Value = CountryName;
HttpContext.Current.Response.Redirect("/en");
}
break;
case "Germany":
if (Country.Value != CountryName)
{
Country.Value = CountryName;
HttpContext.Current.Response.Redirect("/de");
}
break;
case "Sweden":
if (Country.Value != CountryName)
{
Country.Value = CountryName;
HttpContext.Current.Response.Redirect("/se");
}
break;
case "Norway":
if (Country.Value != CountryName)
{
Country.Value = CountryName;
HttpContext.Current.Response.Redirect("/no");
}
break;
default:
if (Country.Value != CountryName)
{
Country.Value = CountryName;
//HttpContext.Current.Response.Redirect("http://www.google.com");
}
break;
}
}
else if (loadedArgs.pageview.Area.ID != 2)
{
HttpContext.Current.Response.Redirect("/choose-country");
}
Further more I also would like to know what could be other possible ways to handle this scenario in more better way so this code don't run on every page load once the cookies are set. Many thanks in advance.
While a domain (or domain name) is used to create a memorable address for a website, a subdomain (for example, mysubdomain.mydomain.com) can be used to do the same for a specific website section.
Under Modify a Subdomain, locate the domain you want to redirect, then click its Manage Redirection link on the right. In the text box, type the URL you would like visitors to be redirected to if they go to the subdomain sample1.hgexample.com. Click Save. You will see the redirected URL under the Redirection column.
I do not have access to your code, but, if I am reading your code right the fix for the redirect issue is to check for the cookies existence before any creation/ redirection logic. I made some changes, please give this a try and let me know of any issues.
var context = HttpContext.Current;
var cookieName = "Country";
var ip = context.Request.UserHostAddress;
if (!string.IsNullOrWhiteSpace(ip) && ip != "127.0.0.1")
{
//If the cookie is present (means cookie is set already) then return
if (context.Request.Cookies[cookieName] != null)
{
return;
}
string countryName;
using (var client = new WebServiceClient(xxxxx, "xxxxxxxx"))
{
var ipCountry = client.Country(ip);
countryName = ipCountry.Country.Name;
}
context.Response.Cookies.Add(new HttpCookie(cookieName)
{
Value = countryName,
Expires = DateTime.Now.AddDays(365)
});
switch (countryName)
{
case "Denmark":
context.Response.Redirect("/");
break;
case "United Kingdom":
context.Response.Redirect("/en");
break;
case "Germany":
context.Response.Redirect("/de");
break;
case "Sweden":
context.Response.Redirect("/se");
break;
case "Norway":
context.Response.Redirect("/no");
break;
default:
//context.Response.Redirect("http://www.google.com");
break;
}
}
else if (loadedArgs.pageview.Area.ID != 2)
{
context.Response.Redirect("/choose-country");
}
Thank you, Soma.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With