For large and medium Web sites, in order to enhance the user experience, often need to be based on the different city site users to push or display the corresponding personalized content, such as for some large portal news will have the function of the city site, if not set the corresponding city site, the default is based on the user access to the city of the IP address automatically set This article mainly through the custom extension IHttpModule interface, considers the performance IP database mainly uses Qqwry pure IP database, mainly realizes the function which automatically jumps to the specified page according to the IP address or the address segment or the IP host city (supports Nginx as the front-end reverse proxy server), The core code for the Websiteskip component is as follows:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Using System.Xml;
Using System.IO;
Using System.Net;
Using System.Text.RegularExpressions;
Using NetOpen_System.Component.QQWry;
Namespace Netopen_system.component
{
public sealed class Websiteskiphttpmodule:ihttpmodule
{
#region IHttpModule Members
public void Dispose ()
{
}
public void Init (HttpApplication context)
{
Context. BeginRequest + = new EventHandler (context_beginrequest);
}
#endregion
void Context_beginrequest (object sender, EventArgs e)
{
Try
{
if (HttpContext.Current.Request.IsLocal)//ignores local computer requests
Return
string ip = HttpContext.Current.Request.UserHostAddress;
string ip = httpcontext.current.request.servervariables["REMOTE_ADDR"]. ToString ();
String IP = string. Empty;
if (httpcontext.current.request.servervariables["http_x_real_ip"]!= null)
{
ip = httpcontext.current.request.servervariables["Http_x_real_ip"]. ToString ();
}
else if (httpcontext.current.request.servervariables["http_x_forwarded_for"]!= null)
{
ip = httpcontext.current.request.servervariables["Http_x_forwarded_for"]. ToString ();
}
else if (httpcontext.current.request.servervariables["Http_via"]!= null)
{
ip = httpcontext.current.request.servervariables["Http_x_forwarded_for"]. ToString ();
}
Else
{
ip = httpcontext.current.request.servervariables["REMOTE_ADDR"]. ToString ();
}
Qqwrylocator Qqwry = new Qqwrylocator (HttpContext.Current.Server.MapPath (@ "~\ipdata\qqwry.dat"));
iplocation ipaddress = qqwry.query (IP); Querying an IP address
String ls_city = IPAddress. Country;
String ls_urlfrom = String. Empty;
String Ls_urlto = String. Empty;
string ls_url = HttpContext.Current.Request.Url.AbsoluteUri;
String Ls_useragentkeyword = String. Empty;
Excludeuseragentmatchengine Em = Websiteskipconfiguration.getconfig (). excludeuseragents;
if (Em.ExcludeUserAgentList.Count > 0)
{
foreach (Excludeuseragent ua in Em.excludeuseragentlist)
{
if (HttpContext.Current.Request.UserAgent.Contains (Ua.keyword))
{
Return
}
}
}
Urlmatchengine pu = websiteskipconfiguration.getconfig (). Skipedurls;
if (PU. Urllist.count > 0)
{
foreach (Skipedurl sk in Pu. Urllist)
{
if (ls_city. Contains (SK. IpCity))
{
if (SK. Urlfrom.length > 0)
{
if (SK. Urlfrom.contains (Ls_url) &&!ls_url. Contains (SK. Outkeyword))
{
if (SK. Urlto.length > 0)
{
HttpContext.Current.Response.Redirect (SK. Urlto, True);
}
Break
}
}
Break
}
}
}
if (Websiteskipconfiguration.getconfig (). Ipchecks.getipin (IP))
{
Ls_urlfrom = Websiteskipconfiguration.getconfig (). IpChecks.UrlFrom.Trim ();
Ls_urlto = Websiteskipconfiguration.getconfig (). IpChecks.UrlTo.Trim ();
if (Ls_urlfrom. Length > 0)
{
if (Ls_urlfrom. Contains (Ls_url) &&!ls_url. Contains (Websiteskipconfiguration.getconfig (). Ipchecks.outkeyword))
{
if (Ls_urlto. Length > 0)
{
HttpContext.Current.Response.Redirect (Ls_urlto, true);
}
}
}
}
}
Catch
{
}
}
}
}
In terms of deployment, it is very simple to use the IHttpModule interface and add the configuration of this component to the HttpModule node in Web.config, access restrictions or allow parameters can be netopen_ SystemWebsiteSkip.cfg.xml, the following is a simple configuration example:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<NetOpen_System>
<WebsiteSkip>
<SkipedUrl>
<add ipcity= "Wenzhou" urlfrom= "http://examplesite.com/Default.aspx,http://examplesite.com/,http://examplesite.cn/, http://www.examplesite.cn "urlto=" http://wz.mainwebsite.pcom/index.aspx "outkeyword=" math "/>
<add ipcity= "Zhenjiang" urlfrom= "http://examplesite.com/Default.aspx,http://examplesite.com/,http://examplesite.cn/, http://www.examplesite.cn "urlto=" http://jszj.mainwebsite.com/index.aspx "outkeyword=" math "/>
</SkipedUrl>
<SkipedIP>
<add ip1= "220.186.0.0" ip2= 220.186.255.255 "urlfrom=" http://examplesite.com/Default.aspx,http:// examplesite.com/,http://examplesite.cn/,http://www.examplesite.cn "urlto=" http://wz.mainwebsite.com/index.aspx " outkeyword= "Math"/>
</SkipedIP>
<ExcludeUserAgent>
<add keyword= "Baiduspider" >
<add keyword= "Sosospider" >
<add keyword= "Sogou web Spider" >
<add keyword= "Sogou Inst Spider" >
<add keyword= "Sogou-test-spider" >
<add keyword= "Sogou Orion spider" >
<add keyword= "Gigabot" >
<add keyword= "0JJJSpider" >
<add keyword= "Sogou Pic Spider" >
<add keyword= "Googlebot" >
<add keyword= "yeti/1.0" >
</ExcludeUserAgent>
</WebsiteSkip>
</WebsiteSkip>
</NetOpen_System>