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

作者: ropon 分类: 经验分享 发布时间: 2018-09-26 14:13

例子1:配置https 301跳转https

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

具体规则

<?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

<?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等图片访问

<?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>

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注