by Igor | Apr 20, 2015 | Hosting, Programming
To redirect web traffic with Cloudflare and ASP.NET first enable IP Geolocation in CloudFlare dashboard:
Related article:
http://netjunky.net/redirect-web-traffic-based-on-country-origin/
To learn more about Cloudflare visit there website:
https://www.cloudflare.com/overview
Once enabled, Cloudflare will then add a header called “CF-IPCountry” to all requests to your website.
In your ASP.NET website code capture that header and redirect traffic.
This can be done on single page or globally in global.asax file.
Code example:
if (Request.Headers.AllKeys.Contains("CF-IPCountry"))
{
Response.Write(Request.Headers["CF-IPCountry"]);
}
Read header and save result in session to use it globally.
Add this code snippet to your global.asax file:
protected void Session_Start(object sender, EventArgs e)
{
if (Request.Headers.AllKeys.Contains("CF-IPCountry"))
{
Session["language"] = Request.Headers["CF-IPCountry"].ToString();
}
}
That’s all folks!
by Igor | Mar 25, 2015 | Hosting
You can enable IP Geolocation to have CloudFlare geolocate visitors to your website and pass the country code to you.
Redirect web traffic based on Country origin to subdomain or to different web site.
You will find the IP geolocation option by going to:
Webiste –>CloudFlare Settings –>IP Geolocation
Direct link is:
https://www.cloudflare.com/cloudflare-settings?z=YOURSITE
Once enabled, Cloudflare will then add a header called “CF-IPCountry” to all requests to your website.
Here are a couple of examples of how to access/store this value:
$country_code = $ENV{"HTTP_CF_IPCOUNTRY"}; # to access in Perl
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"]; // to access in PHP
CloudFlare includes this information for both IPv4 and IPv6 addresses
In your website code capture that header and redirect traffic.
Redirect web traffic based on Country origin PHP sample:
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
if($country_code=="US"){
$location='http://www.mysite.com/us';
}elseif($country_code=="UK"){
$location='http://www.mysite.com/uk';
}else{
$location='http://www.mysite.com/us';
}
header("location:$location");
exit;
Update:
ASP.NET example:
http://netjunky.net/redirect-web-traffic-with-cloudflare-and-asp-net/
Recent Comments