在 URL 重写中排除目录

发布于 2024-12-03 01:08:18 字数 532 浏览 0 评论 0原文

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

,我希望特定目录排除此规则。 我该怎么做呢?

I have this code at my 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>

and I want that specific directories will exclude of this rule.
How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-12-10 01:08:18

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

最好通过附加条件来执行,因为很容易阅读/理解这条规则的含义。


如果您总体上擅长正则表达式,那么您可能更喜欢这种方法:将此类条件移至匹配模式(最终您将得到相同的结果,并且速度会快一点..但更难以阅读):

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

To exclude specific folders (/contact/, /presentation/, /db/site/ -- anything in these folders) from being processed by this rule, you can add one more condition, like this:

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

It is good to do via additional condition because it is easy to read/understand what this rule is about.


If you are good with regex in general, then you may prefer this approach: move such condition into match pattern (you will have the same result in the end and it will be tiny bit faster .. but a bit more difficult to read):

<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>
痴情 2024-12-10 01:08:18

这就是我如何让我的博客目录与 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>

This is how I got my blog directory to work with code ignitor in root and wordpress in /blog/

The blog folder also has the original web.config file which is just the second rule of this file...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文