Why redirect web visitors based on country
There are times when it is useful to redirect a visitor to different default web page based on the visitor’s country of origin. One practical usage is to redirect visitor redirect your website visitors to specific posts or web pages based on IP address and make the language automatically switch to the visitor. This article helps you to understand on how to redirect visitors to a locale based on their country of origin using ASP and ActiveX component.
Let us take a simple case study. Company XYZ is multi-national company with major customers from United States and Japan. The company official website is developed in both English and Japanese languages. The default page is in English language and visitor can switch to Japanese by changing the default language option. There exists a potential problem when a Japanese visitor does not understand English and it could not navigate the web site. So let us developed a simple solution to help Company XYZ redirecting all Internet traffic from country Japan to the Japanese language site. Meanwhile it drives the rest traffic to English site.
How to redirect web visitors based on country
In this example, we use a fully functional IP2Location™ ActiveX component available at https://www.ip2location.com/software/active-x to query country by visitor’s IP address. For unregistered component, there is a 5-second delay in every query. First, install the ActiveX component in IIS web server. It could be as simple as running a command in DOS prompt.
C:\> regsvr32 ip2location.dll
Sample Code:
<% ' Create server-side object Set ipObj = Server.CreateObject("IP2Location.Country") ' Initialize IP2Location object If ipObj.Initialize("demo") <> "OK" Then response.write "IP2Location Initialization Failed. End If ' Get visitor's IP address IPaddr = Request.ServerVariables("REMOTE_ADDR") ' Detect visitor's country of origin by IP address CountryName = ipObj.LookUpShortName(IPaddr) ' Free IP2Location object Set ipObj = nothing If CountryName = "JP" Then ' Visitor is from Japan ' Redirect the URL to index_jp.htm Response.Redirect "index_jp.htm" Else ' Visitor is not from Japan ' Redirect the URL to index_en.htm Response.Redirect "index_en.htm" End If %>