Web.config 规则实例不完全整理

例子1:配置https 301跳转https

  • stopProcessing 匹配成功不继续匹配
  • ignoreCase 忽略大小写
  • MatchAny 或 MathAll 且
  • negate 取反

具体规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" />
<add input="{HTTP_HOST}" pattern="^www.idiyrom.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.idiyrom.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

例子2:手机站m.idiyrom.com 不跳转到https

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" />
<add input="{HTTP_HOST}" pattern="^m.idiyrom.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.idiyrom.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

例子3:设置仅允许某个目录下某个后缀文件访问 如仅允许上传目录下jpg等图片访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="forbiddenhtml">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="upload" ignoreCase="false" />
<add input="{URL}" pattern="^.*(.jpg)" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="https://www.ropon.top/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>