Asp.net 在URL重写中排除目录

Asp.net 在URL重写中排除目录,asp.net,iis,url-rewriting,web-config,Asp.net,Iis,Url Rewriting,Web Config,我的web.config中有此代码 <rule name="Imported Rule 2" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> &l

我的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>

我希望特定的目录将排除在这条规则之外。
如何执行此操作?

若要排除特定文件夹(
/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>

通过附加条件这样做很好,因为很容易阅读/理解此规则的内容


如果您对正则表达式总体上很在行,那么您可能更喜欢这种方法:将这样的条件移动到匹配模式中(您最终会得到相同的结果,而且速度会稍微快一点,但阅读起来会有点困难):


若要将特定文件夹(
/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>

通过附加条件这样做很好,因为很容易阅读/理解此规则的内容


如果您对正则表达式总体上很在行,那么您可能更喜欢这种方法:将这样的条件移动到匹配模式中(您最终会得到相同的结果,而且速度会稍微快一点,但阅读起来会有点困难):


这就是我如何让我的博客目录与root中的code ignitor和/blog/中的wordpress一起工作的方法

博客文件夹也有原始的web.config文件,这只是该文件的第二条规则

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <rewrite>
        <rules>        
            <rule name="wordpress - Rule 1" stopProcessing="true">
                <match url="^blog" ignoreCase="false"/>
                <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="/blog/index.php"/>
            </rule>
            <rule name="app" patternSyntax="Wildcard">
                <match url="*"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>
    </rules>
</rewrite>


这就是我如何让我的博客目录与root中的code ignitor和/blog/

博客文件夹也有原始的web.config文件,这只是该文件的第二条规则

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <rewrite>
        <rules>        
            <rule name="wordpress - Rule 1" stopProcessing="true">
                <match url="^blog" ignoreCase="false"/>
                <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="/blog/index.php"/>
            </rule>
            <rule name="app" patternSyntax="Wildcard">
                <match url="*"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>
    </rules>
</rewrite>


我对问题域一点也不熟悉,但我只是想提供一种想法,即也许匹配节点的url属性允许您指定完整的正则表达式。如果是这样,你可以在那里列出你的目录。另一个考虑的选项是虚拟目录,但这些都是完全猜测。什么目录?请提供几个例子,比如说我想排除联系和表达,如果我想阻止一个目录查看(我的db目录-访问-这是一个非常小的网站),并且目录位于“db/site”目录,谢谢你的帮助,我几乎不熟悉这个问题域,但我只是想提供一种想法,也许匹配节点的url属性允许您指定完整的正则表达式。如果是这样,你可以在那里列出你的目录。另一个考虑的选项是虚拟目录,但这些都是完全猜测。什么目录?请提供几个例子,比如说,如果我想阻止一个目录查看(我的db目录-访问-这是一个非常小的网站),并且该目录位于“db/site”目录,我也希望排除联系人和陈述,谢谢help@Chaofix如果这解决了你的问题,那么请考虑把这个答案标记为被接受,谢谢。很抱歉回复晚了。如果我尝试转到“/contact/admin”,我会从根目录中获取“default.asp”-“contact”目录下的每一页都返回根目录的“default.asp”。。。请注意-我尝试了两个示例-正则表达式eample返回404错误…:-(MyCyType =“ISDirectory”NeadTale=“true”)排除了该规则中的所有目录吗?@ CHOFIX,如果这解决了您的问题,那么请考虑将此答案标记为“接受”,THNX。抱歉,对于最近的答复。如果我尝试去“/联系/管理”,我得到“默认.asp”格式的根目录-在“联系”目录下的每个页面返回“默认。ASP”。请注意-我尝试了两个示例-regex eample返回404错误…:-(matchType=“IsDirectory”negate=“true”是否将所有目录从该规则中排除?