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

Seb Nilsson picture Seb Nilsson · Oct 4, 2012 · Viewed 33.4k times · Source

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>

Answer

Seb Nilsson picture Seb Nilsson · Oct 15, 2012

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.