We help IT Professionals succeed at work.

redirecting user to the correc domain based on the country of access

jbajaj
jbajaj used Ask the Experts™
on
I have two portal omvedstore.com and omvedstore.in hosted seperately on two shared servers.

Case 1: Now if a user from India tries to access omvedstore.com he is redirected to omvedstore.com.

Case 2: If a user tries to access omvedstore.in form overseas ( any county but india) he gets redircted to omvedstore.com

In order to do so I have put the code snipsets on both the server, which works well for case 1. However it fails for case 2

Below is the code

Code in Omvedstore.com ( checks is the user is from india . If yes sends him to omvedstore.in else to omvedstore.com ). This works well

string ipAddress = Request.ServerVariables["REMOTE_ADDR"];

                    ip_to_country data = stmngr.GetIPtoCountry(ipAddress);
                    if (data != null)
                    {
                        if (data.TwoCountryCode.ToLower().Trim() == ("IN").ToLower().Trim())
                        {
                            if (ConfigurationManager.AppSettings["IndiaSiteUrl"] != null &&
                            !String.IsNullOrEmpty(ConfigurationManager.AppSettings["IndiaSiteUrl"].ToString()))
                                Response.Redirect(String.Format("{0}", ConfigurationManager.AppSettings["IndiaSiteUrl"].ToString()));
                        }
                        else
                        {
                            Response.Redirect(String.Format("{0}", ConfigurationManager.AppSettings["IndiaSiteUrl"].ToString()));
                        }
                    }


Code in omvedstore.in ( checks if user is from overseas and redirects him to omvedstore.com . This doesnt work. The user stays on omvedstore.in )

string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
                    ip_to_country data = stmngr.GetIPtoCountry(ipAddress);
                    if (data != null)
                    {
                        if (data.TwoCountryCode != "IN")
                        {
                            if (ConfigurationManager.AppSettings["GlobalSiteUrl"] != null &&
                            !String.IsNullOrEmpty(ConfigurationManager.AppSettings["GlobalSiteUrl"].ToString()))
                                Response.Redirect(String.Format("{0}?cntCode=US", ConfigurationManager.AppSettings["GlobalSiteUrl"].ToString()));
                        }
                    }


Please let me know what the issue might be.

Else you can suggest me a better code as well

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This is not an IIS issue.

You are doing the redirect in .net so you need soem .net experts to look at this.

I have requested attention ofr you and a mod shoudl add this to the .net groups.
Software Architect
Commented:
There are several conditions in your second code block that could be preventing it from hitting the Response.Redirect.

First, the two pieces of code are inconsistent in the way they are testing the country code:

data.TwoCountryCode.ToLower().Trim() == ("IN").ToLower().Trim()

vs

data.TwoCountryCode != "IN"

The first one is wasteful (given that you're using a hard-coded value just use the case you already need) and the second one doesn't take into account differences in case. Both should probably read:

data.TwoCountryCode.ToLower().Trim() <condition> "in"

That will at least get that working consistently.

Are you absolutely, positively, 100% sure that ConfigurationManager.AppSettings["GlobalSiteUrl"] is returning a non-null, non-empty result? If this is only happening on your production environment and you have no way to simulate it in development you may have to put some debugging code in temporarily to ensure that you're getting the values you think you're getting. I would suggest the following. If Response.Write won't work for you find somewhere else to write the debug information that you can access.

if (data != null)
{
    Response.Write(data.TwoCountryCode);

    if (data.TwoCountryCode.ToLower().Trim() != "in")
    {
        Response.Write( ConfirmationManager.AppSettings["GlobalSiteUrl"] );

        if (ConfigurationManager.AppSettings["GlobalSiteUrl"] != null &&
           !String.IsNullOrEmpty(ConfigurationManager.AppSettings["GlobalSiteUrl"]))
        {
            Response.Redirect(String.Format("{0}?cntCode=US", ConfigurationManager.AppSettings["GlobalSiteUrl"]));
        }
    }
}

Open in new window