Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Redirect users by IP Address View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 5/4/2011 10:18:56 PM
   
Redirect users by IP Address
Hi guys,

How can I make our users to go to a specific page based on the country.
e.g.: We would like Australian customers to go to abc page while new zealand customers to go on the xyz page.
How can I do this?

Also we have 10 website on the same instance of kentico and we only want to do it for one website not all the 10.


I tried the below link but it doesn't work:
http://devnet.kentico.com/Knowledge-Base/Security-and-Permissions/How-to-redirect-site-visitor-to-other-location-in-.aspx

Using kentico cms 5.5 R2.

Thanks
Gitesh Shah

User avatar
Kentico Support
Kentico Support
kentico_radekm - 5/18/2011 3:33:52 AM
   
RE:Redirect users by IP Address
Hello.

The link you mentioned refers to good and usual solution for this task. Application_BeginRequest method in App_Code/global.asax file is a good place, where you can check visitor´s IP and do proper action, e.g. redirection. Of course, the same logic can be used on any web part or page, however global.asax is the best place.

What exactly does not work for you, please? I tried it on my machine and it works fine. Of course, that article is written for older version and e.g. IP2CountryHelper class is static in 5.5R2, so you do not need to create an instance of it, and you can use it directly like this:

string currentCountryName = CMS.WebAnalytics.IP2CountryHelper.GetCountryByIp(string IP);

Best Regards,
Radek Macalik

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 6/7/2011 12:28:40 AM
   
RE:Redirect users by IP Address
Hi Radek,

Thanks for the code. It works for me in New Zealand.

But when I try for US, it doesn't work.
My Website URL is www.kudosweb.com.
If US customers come to this URL it should redirect to www.kudosweb.com/US

This is the code I have written in app_code/global.asax.cs:



private static void RewriteUrl(string relativePath, ExcludedSystemEnum excludedEnum, ViewModeOnDemand viewMode, SiteNameOnDemand siteName)
{

string siteName12 = CMSContext.CurrentSiteName;
if (siteName12 == "kudosweb")
{
string ip = HttpContext.Current.Request.UserHostAddress;
string currentCountryName = IP2CountryHelper.GetCountryByIp(ip);
string currentDomain = CMS.GlobalHelper.UrlHelper.GetCurrentDomain().ToLower();

switch (currentCountryName)
{
case "UNITED STATES":
if (currentDomain == "kudosweb.com")
{
URLRewriter.Redirect("http://www.kudosweb.com/US/");
}
if(currentDomain == "www.kudosweb.com")
{
URLRewriter.Redirect("http://www.kudosweb.com/US/");
}
break;
}
}
else
{
}

bool checkParameters = true;






Could you please see , if there's anything wrong in the code.

Thanks
Gitesh Shah

User avatar
Kentico Support
Kentico Support
kentico_radekm - 6/8/2011 1:58:11 AM
   
RE:Redirect users by IP Address
Hello.

Did you try to debug it to see, what is the value of "currentCountryName" variable in code below?

...
switch (currentCountryName)
...


I mean to see whether it matches "UNITED STATES" string? And what is the value of "currentDomain" variable in if-conditions within proper case element?

If it does not work, some of these values will be wrong. Debug should show which one and what is real value within. Could you please check it and update me? Thank you.

Best Regards,
Radek Macalik

User avatar
Certified Developer v7
Certified  Developer v7
Gitesh - 6/14/2011 11:21:22 PM
   
RE:Redirect users by IP Address
Hi Radek,

I am in New Zealand. Will I be able to debug for United states? Also we don't have visual studio installed on the server. Is there any other way?

Does it work for everyone for the string "UNITED STATES"?

Thanks
Gitesh Shah

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 6/26/2011 7:17:46 PM
   
RE:Redirect users by IP Address
Hi,

You can use some proxy with US-based IP address (lists available here or here, or anywhere else over the web) or change the code to return desired country for your specific single IP address.

If you don't haveVisual Studio on the server for debugging, you can add logging of an event with details (values) to your modified code and then examine the results in Event log. See more details in API Reference for EventLogProvider class, methot LogEvent, usage can be found in many codefiles of the webproject.

Regards,
Zdenek