web.config 重写路径规则

web.config rewrite rule for paths

我正在尝试在 web.config 中创建一个重写规则,它将采用 url,例如 http://example.org/MySite and just redirect them back to the root site such as http://example.org

它还应该忽略特定的文件夹,例如 http://example.org/css or http://example.org/js

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
      <match url="http://example.org/^$" /> <!-- Wrong !! ->
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

这 2 条规则应该就是您所需要的:

<rewrite>
    <rules>
        <rule name="root rule" stopProcessing="true">
            <match url="^$" />
            <action type="None" />
        </rule>
        <rule name="redirect rule" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_URI}" pattern="^/css" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/js" negate="true" />
          </conditions>
          <action type="Redirect" url="/" />
        </rule>
    </rules>
</rewrite>

第一个确保根目录没有被重定向到重定向循环,并停止处理其他可能适用的进一步规则,第二个排除您要排除的文件夹,只需在条件中添加另一行如果有更多您不想重定向到根目录的文件夹。