IIS URL Rewrite: Add trailing slash except for .html and .aspx

asp.netiisurl-rewrite-moduleurl-rewriting

Adding a trailing slash to all URLs through IIS URL Rewrite Module is widely spread, but how do I add exceptions for URLs that ends with .html and .aspx?

Today I have this:

<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <!-- Doesn't seem to be working -->
    <!--<add input="{REQUEST_URI}" pattern="(.*?).html$" negate="true" />-->
    <!--<add input="{REQUEST_URI}" pattern="(.*?).aspx$" negate="true" />-->
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

Best Answer

If you want something done right, you've got to do it yourself, obviously...

Here is the solution to my question:

<rule name="Add trailing slash" stopProcessing="true">
  <match url="(.*[^/])$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
    <add input="{REQUEST_FILENAME}" pattern="(.*?)\.aspx$" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

Update: I blogged about this in more detail.

Related Topic