防止URL重写规则被IIS7中的子目录继承

Ric*_*nks 16 iis rewrite iis-7 url web.config

我有一个 CMS 中干净 URL 的 URL 重写设置,我的 web.config 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Clean URLs" stopProcessing="true">
                    <match url="^([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="?id={R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

它基本上变成index.php?id=somethingsomething清洁的网址。非常简单,而且效果很好。

在 CMS 中很常见,为了防止后端中断,每个子目录都需要<remove name="Clean URLs" /><clear />在其 web.config 中,因此规则不会被继承。

有没有办法在父规则中指定它根本不应该被子规则继承,方法是将规则的范围限制在当前目录?类似的东西<rule name="Clean URLs" stopProcessing="true" inherit="no">将是史诗般的。

Ric*_*nks 14

谷歌搜索 4.5 小时后找到答案!

http://runtingsproper.blogspot.co.uk/2010/04/solved-break-parent-webconfig.html

基本上利用

<location path="." inheritInChildApplications="false"> 
    <system.webServer>
        <!-- ... -->
    </system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)


Bry*_*Way 9

我最近在类似的情况下遇到了这个问题。但是 rjenkins 的答案似乎会导致依赖父设置继承的虚拟应用程序出现问题。

如果您知道重写规则的名称,您可以这样做:

<rewrite>
  <rules>
    <remove name="RewriteNameToDisable" />
  </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

  • 那是我的第一次尝试,但是您必须将它放在每个子目录的 web.config 中,这并不总是实用的。我同意它更干净,但需要更多的工作,特别是当子目录中可能有重写时(例如子文件夹中 cms 的另一个实例) (2认同)