<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="rule1" stopProcessing="true">
<match url="home(/index.php)?$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="admin/index.php{R:2}"/>
</rule>
<rule name="rule2" stopProcessing="true">
<match url="index(/index.php)?$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="main/index.php{R:2}"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
添加规则,使用 <rule>...</rule>
标签对
主要属性说明:
name 属性:规则名称,不能有相同值
stopProcessing 属性:为匹配成功后停止动作,不再往下执行
match 属性:url 为正则表达式,匹配 HTTP 请求的 url 链接
conditions 属性:IsDirectory、IsFile 当请求的路径是文件或目录的话,按服务器默认处理方式
比如,请求的是文件 http://domain.com/home/logo.jgp 直接返回文件,不适用重写规则
Rewrite 属性:执行重写,当 match 匹配成功后执行 url 链接,{R:2} 为匹配成功后的引用变量
如果搞不清 {R:2} 的具体含义,可使用 IIS web 平台安装程序的 URL 重写工具测试
举例说明:
当请求 <span style="color: #008000;">http://domain.com/home/action/view/so </span>首先执行第一条规则
匹配成功,将 URL 链接重新定位到
<span style="color: #008000;">http://domain.com/admin/index.php/action/view/so</span>
此链接为大多服务器支持的 PATHINFO 模式
如果第一条规则匹配不成功,就执行第二条规则,如
<span style="color: #008000;">http://domain.com/index/index.php/action/view/so</span>
会被定位到
http://domain.com/main/index.php/action/view/so
利用正则优化网站链接方式,简便好记,更安全