IIS Forces Slash even with URL Rewrite to remove it

iis-7trailing-slashurl-rewriting

I am unable to remove the trailing slash my site's URLs even with the URL rewrite from: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/.

Frustrating really since it should be so simple but my attempts have not produced any results.

I even went as far as to create a test directory and added file called desktops.aspx and and sub folder called desktops.

without the sub directory "/test/desktops" loads fine since i set the default document to look at desktops.aspx.

with a subfolder created and still referencing "/test/desktops" it forces the slash and looks at the sub directory.

Why does IIS does this since its supposed to look for the file first then the sub directory correct? Are there any settings on the server side that would force back the slash?

URL Rewrite Snippet:

<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.+)/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="_{R:1}" />
</rule>

any help would be welcome

Best Answer

You are using an action of type Rewrite but you want a Redirect.

Change your configuration to:

<rule name="SEO - Remove trailing slash" stopProcessing="false">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Redirect" url="{R:1}" />
</rule>

You also need to change url="(.+)/$" to url="(.*)/$".

TIP:

The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:

Related Topic