17

I have a URL Rewrite setup for clean URLs in a CMS and my web.config looks like this:

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

It basically turns index.php?id=something into something for clean URLs. Very simple and it works well.

As is common in CMSs, to prevent the back-end from breaking, each subdirectory requires either <remove name="Clean URLs" /> or <clear /> in its web.config so the rule isn't inherited.

Is there a way of specifying in the parent rule that it shouldn't be inherited by its children at all by somehow limiting the rule's scope to only the current directory? Something like <rule name="Clean URLs" stopProcessing="true" inherit="no"> would be epic.

Rich Jenks
  • 431
  • 1
  • 3
  • 8

2 Answers2

15

Found the answer after 4.5 hours of Googling!

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

Basically taking advantage of

<location path="." inheritInChildApplications="false"> 
    <system.webServer>
        <!-- ... -->
    </system.webServer>
</location>
Rich Jenks
  • 431
  • 1
  • 3
  • 8
  • It took me 5 hours to finally run into this solution and it finally worked for me as well. You're a life saver. – Eliyah Sep 18 '22 at 19:45
10

I recently ran into this problem, in a similar situation. But the answer from rjenkins would seem to cause problems with virtual applications that relied on inheritance of parent settings.

If you know the name of the rewrite rule you can just do this:

<rewrite>
  <rules>
    <remove name="RewriteNameToDisable" />
  </rules>
</rewrite>
Bryan Way
  • 203
  • 2
  • 6
  • 2
    That was my first attempt, but you have to put that in the web.config for every subdirectory and that's not always practical. I'd agree it's cleaner, but it's more work, especially when there may be rewrites in subdirectories (e.g. another instance of the cms in a subfolder) – Rich Jenks Jun 26 '15 at 07:27
  • This worked on a virtual application in azure. – Allen Jun 30 '21 at 08:10