在URL重写中排除目录

Cha*_*fix 1 asp.net iis web-config url-rewriting

我在web.config上有这个代码

<rule name="Imported Rule 2" stopProcessing="true">
  <match url="^(.*)$" ignoreCase="false" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
  </conditions>
  <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>
Run Code Online (Sandbox Code Playgroud)

我希望特定目录将排除此规则.我该怎么做?

Laz*_*One 14

排除特定的文件夹(/contact/,/presentation/,/db/site/-由该规则正在处理这些文件夹中的任何东西),你可以添加更多的条件,就像这样:

<rule name="Imported Rule 2" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(contact|presentation|db/site)" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>
Run Code Online (Sandbox Code Playgroud)

通过附加条件做好事是因为它易于阅读/理解这条规则的含义.


如果你对正则表达式一般都很好,那么你可能更喜欢这种方法:将这样的条件转换为匹配模式(最终你会得到相同的结果,它会更快一些......但是更难以阅读):

<rule name="Imported Rule 2" stopProcessing="true">
    <match url="^(?!(?:contact|presentation|db/site)/)(.*)$" ignoreCase="false" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>
Run Code Online (Sandbox Code Playgroud)