Seb Nilsson
Seb Nilsson

Reputation: 26438

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

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>

Upvotes: 24

Views: 40842

Answers (8)

MyPhuong Tran
MyPhuong Tran

Reputation: 11

You can try this:

        <conditions>
            <add input="{URL}" pattern="(.*)\.(.*)$" negate="true" />
        </conditions>

Upvotes: 0

MyPhuong Tran
MyPhuong Tran

Reputation: 11

            <conditions>
                <add input="{URL}" pattern="(.*)\.(.*)[a-z]$" negate="true" />
                <add input="{URL}" pattern="(.*)\.(.*)[0-9]$" negate="true" />
            </conditions>

except for .html and .aspx and .woff2

Upvotes: 1

Johan B
Johan B

Reputation: 890

To prevent POST, DELETE and other REST method calls without a trailing slash from erroneously becoming a GET request through the redirect consider adding the following condition:

<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="GET" ignoreCase="true" />

Upvotes: 0

Garland Pope
Garland Pope

Reputation: 3271

To prevent all files from having a slash added, I changed the match rule to this:

<match url="^([^.]*[^/])$" />

That applies the rule only to paths that include any number of non-dot characters that does not end in a slash. So any path that includes a dot (e.g. xxx.html, xxx.aspx, etc.) would be excluded without needing any additional negation rule.

Looking for the presence of a dot in the match rule allowed me to completely remove the condition rules that use match types IsFile and IsDirectory. Those match types are only allowed in distributed rules (web.config), not in the global rules (applicationHost.config), so I had been forced to replicate this rule for every site instead of applying it to all sites using a global rule. By modifying the regex in the match rule to exclude files and removing the IsFile and IsDirectory conditions, I was able to create a global rule instead of having multiple distributed rules.

Upvotes: 1

Giscard Biamby
Giscard Biamby

Reputation: 4619

Varying the other answers, I used this so I wouldn't have to specify a list of file extensions:

<!-- Ensure trailing slash -->
<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="(.*?)\.[a-zA-Z]{1,4}$" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule> 

Upvotes: 20

Denis
Denis

Reputation: 4998

We add multiple extensions like this:

<add input="{URL}" negate="true" pattern="((.+).(jpg|ico|gif|js|png|htm|css|html))" ignoreCase="true" />

Upvotes: 3

Seb Nilsson
Seb Nilsson

Reputation: 26438

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.

Upvotes: 28

Colin Wiseman
Colin Wiseman

Reputation: 868

This almost worked for me. I had to change it to

<add input="{URL}" pattern="(.*?)\.html$" negate="true" />
<add input="{URL}" pattern="(.*?)\.aspx$" negate="true" />

Otherwise thanks for this!

Upvotes: 0

Related Questions