这里要求对域名进行重写,实现http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重写。
第一:域名
首先域名要支持泛解悉,就是域名解悉的主机名为星号*,例:*.abc.com。如下图

这样能保证你在浏览器地址栏输入任何前缀,DNS都会把它们指向到你指定的IP地址上。
第二:IIS设置(Win2003 + IIS 6为例)
(1)网站必须为Web服务器的默认站点,即端口号为80,主机头为空的站点。如下图所示。

该站点接收所有对该服务器的HTTP请求(其它设置为主机头的站点除外)。所以任何二级域名访问该服务器都会由该站点进行处理。
(2)另外要在站点的“通配符应用程序映射”列表中添加ASP.NET的Web请求处理程序aspnet_isapi.dll。如下图所示。

在这里的设置,是让该站点接到的所有请求都交给aspnet_isapi.dll处理。
第三:修改Microsoft的URLRewriter。
运行开源项目URLRewriter。这里需要修改两个地方:
(1)BaseModuleRewriter.cs类
01 |
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
|
05 |
HttpApplication app = (HttpApplication) sender;
|
07 |
//Rewrite(app.Request.Path, app);
|
09 |
Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 这里修改了
|
这里将app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
(2)ModuleRewriter.cs类
01 |
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
|
05 |
// log information to the Trace object.
|
07 |
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
|
11 |
// get the configuration rules
|
13 |
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
|
17 |
// iterate through each rule...
|
19 |
for (int i = 0; i < rules.Count; i++)
|
23 |
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
|
25 |
//string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
|
27 |
string lookFor = "^" + rules[i].LookFor + "$"; // ## ## ## 这里修改了
|
31 |
// Create a regex (note that IgnoreCase is set...)
|
33 |
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
|
37 |
// See if a match is found
|
39 |
if (re.IsMatch(requestedPath))
|
43 |
// match found - do any replacement needed
|
45 |
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
|
49 |
// log rewriting information to the Trace object
|
51 |
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
|
57 |
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
|
59 |
break; // exit the for loop
|
67 |
// Log information to the Trace object
|
69 |
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
|
相关文章: