利用Open Source Intelligencia.UrlRewriter实现重写url
实现效果如下:
~/video 到 ~/*.aspx(或者~/tags/*.aspx)
~/a.htm(或者自定义的扩展名) 到 ~/a.aspx
~/class/(.+) 到 ~/Class/Default.aspx?classid=$1
等效果
方法如下:
(1)下载Intelligencia.UrlRewriter.dll文件
(2)将Intelligencia.UrlRewriter.dll引用到你的website程序中
(3)配置webconfig
1)添加 Configuration section handler:
<configSections>
<section
name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
2)添加 UrlRewriter mapper HttpModule:
<system.web>
<httpModules>
<add
type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"
name="UrlRewriter" />
</httpModules>
</system.web>
3)添加 重写规则到 web.config的configuration结点中,如下
<rewriter>
<rewrite url="/tags/(.+)" to="/tagcloud.aspx?tag=$1" />
</rewriter>
整个web.config文件
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(4)如果需要重写为htm、其它扩展名或者没有扩展名的url时,需要在IIS中设置
设置方法如下:
在windowXP系统中:
1)Open Up IIS and Navigate to the “Home Directory Tab”
2)Select “Configuration”
3)Click “Add” and enter “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll” in the Executable box. For the file extension, enter “.*”. Finally, make sure that “Check that file exists” is not checked.
在window 2003server中请参考:
http://urlrewriter.net/index.php/support/installation/windows-server-2003/
示例代码下载