您的位置:首页 > 其它

IIS7/Win7中使用Intelligencia.UrlRewriter实现url重写时候不能正常使用原因

2012-02-27 15:28 686 查看
一,具体web.config文件怎么配置

第一步,在配置文件configSections结点中添加下面配置

<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />

第二步,在httpModules结点内添加下面配置

<!--配置url重写指定具体的处理组件-->

<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />

第三步,在modules 结点添加runAllManagedModulesForAllRequests属性

<modules runAllManagedModulesForAllRequests="true">

第四步,在modules结果添加以下配置

 <!--您的自定义IIS重写模块操作-->

 <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>

到现在为止,你的Intelligencia.UrlRewriter重写组件就可以成功在iis7中使用了。

二,具体怎么配置重写规则可以参照下面的:

重写HTML的规则示例:

(1)

注意:

当使用通配符映射或者使用ASP.NET处理所有HTTP请求的时候,IIS的默认文档机制也就失去了该有的作用,以下代码就是用来重新实现默认文档的方法:

<rewrite url="^(.*)/(\?.+)?$" to="$1/default.aspx$2" />注意:使用“processing="restart"”的时候将会导致重写引擎从头开始执行所有的重写规则,此时应当注重包含“/default.aspx”字符串的处理。

如果你需要同时支持多个默认文档,那么以上代码需要修改为:

<if url="^(.*)/(\?.+)?$">

<rewrite exists="$1/default.aspx" to="$1/default.aspx$2" />

<rewrite exists="$1/index.aspx" to="$1/index.aspx$2" />

<rewrite exists="$1/index.html" to="$1/index.html$2" />

</if>编辑以上规则代码的时候,尤其要注意确认文件是否存在。

此外,在使用通配符映射或者使用ASP.NET处理所有HTTP请求的时候,你会发现.gif、.css等文件无法正常访问,这是因为此时对这些文件类型的处理都被ASP.NET所拦截。修正方法如下:

<rewrite

url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.css|\.js)(\?.+)?)$"

to="$1" processing="stop" />两个比较特殊的正则表达式字符是“^”和“$”(不是必须的),“^”代表URL的开头,“$”代表URL的结尾。使用这两个符号可以使您更加精确的控制重写动作,以确保程序所匹配的URL正是您想处理的。

“~/”表示当前网站应用程序运行所在的虚拟根目录,当您把网站应用程序安装于虚拟目录(或者非根目录)的时候,这尤其有用,而无须重新编写任何代码来替换您的虚拟根路径。

模式匹配在处理查询字串(QueryStrings)的时候尤其有用,这可以让您的URL去掉类似于“?id=3”的代码段,这非常有用,可以让你轻松的实现伪静态。

下面列出两个我们认为非常有特色的重写规则:

(1) 当目标URL不包含自定义查询字串的时候:

<rewrite url="^~/mypage(\?.+)?$" to="~/default.aspx$1" />此时“$1”匹配的是(\?.+)?,也就是所有的查询字串。

(2) 当目标URL包含自定义查询字串的时候:

<rewrite url="^~/mypage(\?(.+))?$"

to="~/default.aspx?page=mypage&$2" />注意此时需要使用“&”来代替“&”,这是XML文档语法的需要。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bl_song/archive/2009/11/03/4761343.aspx

(2)

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><rewriter>

<rewrite url="~/default.html$" to="~/default.aspx" processing="stop" />

<rewrtie url="^~/(\d+)/(\d+).html" to="~/Default.aspx?id=$1&artcleid=$2" />//多个参数

<rewrite url="~/news_([0-9]+).html$" to="~/Article/ArticleShow.aspx?id=$1" processing="stop" />

<rewrite url="~/Result_([0-9]+).aspx$" to="~/Job/Company/Result.aspx?cid=$1" processing="stop" />

<rewrite url="~/company_([0-9]+).html$" to="~/Company/$1/index.html" processing="stop" />

<rewrite url="~/Resume/([0-9]+)_([0-9]+).html$" to="~/Resume/$1/Resume_$2.html" processing="stop" />

<rewrite url="~/([0-9]+)_([0-9]+).html$" to="~/company/$1/job_$2.html" processing="stop" />

<rewrite url="~/job_([0-9]+).html$" to="~/company/$1/jobs_$1.html" processing="stop" />

<rewrite url="~/news_l([0-9]+)_1.html$" to="~/company/$1/$1_news.html" processing="stop" />

<rewrite url="~/news_([0-9]+)v([0-9]+).html$" to="~/company/$1/n_$2.html" processing="stop" />

<rewrite url="~/contact_([0-9]+).html$" to="~/company/$1/contact.html" processing="stop" />

<rewrite url="~/a_(.+)_(.+).aspx$" to="~/a.aspx?id=$1&id1=$2" processing="stop" />//多个参数

三,具体在配置之前可以出现以下两种错误

1,这是因为没有在modules结点添加以后配置

 <!--您的自定义IIS重写模块操作-->

 <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>,



2,是因为没有添加以下配置:就是没有添加这个属性
<modules runAllManagedModulesForAllRequests="true">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: