Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Forces Slash even with URL Rewrite to remove it

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

like image 947
fseminario Avatar asked Feb 11 '13 16:02

fseminario


2 Answers

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:

like image 113
cheesemacfly Avatar answered Oct 15 '22 03:10

cheesemacfly


I was having this same problem and here is what I found.

My intent was to use this rule on an MVC website but I didnt want to test in production so I tested the rule on a site already setup which happened to be web forms asp.net.

I encountered the same problem as you. Navigating to www.example.com/test redirected to www.example.com/test/ even with the rule in place.

So I noticed the conditions to check if the requested url is a file or directory and I removed them.

Now going to www.example.com/test/ redirected to www.example.com/test. Yay! No. IIS automatically added another redirect back to www.example.com/test/ resulting in a redirect loop. Boo.

I then found this article https://support.microsoft.com/en-us/kb/298408 which relates to IIS 6 but is obviously still an issue.

So because I am in an asp.net web forms site, in my case /test is a physical directory and there is something in IIS that forces the trailing slash for directories. And sorry to say but I couldn't find a way to easily turn it off.

However! My requirement was for MVC and configured routes that are NOT directories. So I tried it again on a MVC website and the redirect to remove the trailing slash worked perfectly.

The rule I ended up with is:

<rule name="RemoveDatSlash" stopProcessing="true">
    <match url="(.*)/$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

Hope this helps!

like image 40
johnw182 Avatar answered Oct 15 '22 03:10

johnw182