bkhosh2 bkhosh2 - 3 months ago 30
ASP.NET (C#) Question

How to Exclude Directories from Rules in Web.config (ASP.NET)

I have these rules set in my Web.config:

<rules>
<rule name="replacedash">
<match url="(.*)(-)(.*)\.aspx$" />
<action type="Redirect" url="{R:1}{R:3}" redirectType="Permanent" />
</rule>
<rule name="extensionless" stopProcessing="true">
<match url="(.*)\.aspx$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="removeextension" enabled="true">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>


I've upgraded my website from plain ASP.NET to ASP.NET MVC and I've written these rules just in case there are still some old cached links in search engines with .aspx extensions; however, there are some directories on my web server that still contain .aspx files for good reasons and my rules are rewriting those extensions as well.

How do I exclude these folders from inheriting my rules?

Thank you

Answer

Based on the following solution here:

How to exclude a directory with IIS URL rewriting?

You could do something like this:

<rules>
    <rule name="ignore web forms folder 1" stopProcessing="true">
        <match url="^webformsfolder1/" />
        <action type="None" />
    </rule>
    <rule name="ignore web forms folder 2" stopProcessing="true">
        <match url="^webformsfolder2/" />
        <action type="None" />
    </rule>
    <rule name="ignore web forms folder 3" stopProcessing="true">
        <match url="^webformsfolder3/" />
        <action type="None" />
    </rule>
    <rule name="replacedash">
      <match url="(.*)(-)(.*)\.aspx$" />
      <action type="Redirect" url="{R:1}{R:3}" redirectType="Permanent" />
    </rule>
    <rule name="extensionless" stopProcessing="true">
          <match url="(.*)\.aspx$" />
          <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="removeextension" enabled="true">
        <match url=".*" negate="false" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:0}" />
    </rule>
</rules>
Comments