ASP.NET 使用URLRewriter重写二级域名
来源:互联网 发布:js判断变量是否是数组 编辑:程序博客网 时间:2024/05/16 07:09
转载自: http://www.cnblogs.com/zhhh/archive/2011/03/18/1987985.html
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true
这里要求对域名进行重写,实现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)
02
03
{
04
05
HttpApplication app = (HttpApplication) sender;
06
07
//Rewrite(app.Request.Path, app);
08
09
Rewrite(app.Request.Url.AbsoluteUri, app);
// ## ## ## 这里修改了
10
11
}
这里将app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
(2)ModuleRewriter.cs类
01
protected
override
void
Rewrite(
string
requestedPath, System.Web.HttpApplication app)
02
03
{
04
05
// log information to the Trace object.
06
07
app.Context.Trace.Write(
"ModuleRewriter"
,
"Entering ModuleRewriter"
);
08
09
10
11
// get the configuration rules
12
13
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
14
15
16
17
// iterate through each rule...
18
19
for
(
int
i = 0; i < rules.Count; i++)
20
21
{
22
23
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
24
25
//string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
26
27
string
lookFor =
"^"
+ rules[i].LookFor +
"$"
;
// ## ## ## 这里修改了
28
29
30
31
// Create a regex (note that IgnoreCase is set...)
32
33
Regex re =
new
Regex(lookFor, RegexOptions.IgnoreCase);
34
35
36
37
// See if a match is found
38
39
if
(re.IsMatch(requestedPath))
40
41
{
42
43
// match found - do any replacement needed
44
45
string
sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
46
47
48
49
// log rewriting information to the Trace object
50
51
app.Context.Trace.Write(
"ModuleRewriter"
,
"Rewriting URL to "
+ sendToUrl);
52
53
54
55
// Rewrite the URL
56
57
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
58
59
break
;
// exit the for loop
60
61
}
62
63
}
64
65
66
67
// Log information to the Trace object
68
69
app.Context.Trace.Write(
"ModuleRewriter"
,
"Exiting ModuleRewriter"
);
70
71
}
这里将string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了 string lookFor = "^" + rules[i].LookFor + "$";
这两个地方修改完以后,生成项目。将项止目bin/Debug目录下的URLRewriter.dll文件Copy到我们要重写URL的项目中。
第四:配置项目
(1)在web.config文件的configSections节点下添加如下一行代码
1 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
这里配置一个重写配置的类
(2)修改httpModules节点,在里面添加一行配置代码
1 <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
(3)在主节点configuration节点下添加路由规则,代码如下:
1 <!-- URL重写 将捕获页面转发到实际地址 -->
2
3 <RewriterConfig>
4
5 <Rules>
6
7 <RewriterRule>
8
9 <LookFor>http://(/w+).abc.com/</LookFor>
10
11 <SendTo>~/Defa.aspx?id=$1</SendTo>
12
13 </RewriterRule>
14
15 </Rules>
16
17 </RewriterConfig>
18
19 <!-- URL重写 将捕获页面转发到实际地址 ( 结束 ) -->
代码里一个RewriterRule节点就是一个规则,这时只有一个,即把域名中的主机头部分做为Defa.aspx页面的id参数的值发送给Defa.aspx页面。
注意:这里LookFor节点里的http://(/w+).abc.com/不能少了最后的反斜框
OK,一切完工
发布,上传到服务器,测试一下,如图
- ASP.NET 使用URLRewriter重写二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net 2.0 通过url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名
- asp.net用url重写URLReWriter实现任意二级域名(续)
- Asp.net 用url重写(URLReWriter)实现任意二级域名
- 创建动态数组
- Notice: Use of undefined constant 错误 调整 php.ini文件
- 文件系统对服务器的影响
- 第一篇
- java操作txt或xls文件批量导入数据
- ASP.NET 使用URLRewriter重写二级域名
- 编码剖析Spring装配基本属性的原理---自定义的Spring容器
- POJ_2479 最大连续子序列和
- 海明校验码
- POJ-1338-Ugly Numbers-丑数
- 【转】XML基础知识
- 添加一个自定义世界时间 地区
- Task和Activity相关
- android opengl es 三角体,正方体自动旋转