9

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

fseminario
  • 801
  • 1
  • 9
  • 13

3 Answers3

10

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:

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • Makes sense. Thanks! I actually had been trying with a redirect with all the other rules but forgot to check for it on this one. Anyway, I updated the rule per your instructions but i still get redirected to the slash. it is the only rule being used right now. /test/desktops redirects to /test/desktops/ – fseminario Feb 11 '13 at 20:08
  • I made a mistake, you should use `(.*)/$` as a pattern (see my updated answer). I should have followed my own advice... :) – cheesemacfly Feb 11 '13 at 20:17
  • hehe. Gotcha. I updated but still not working. The slash is forced back on. – fseminario Feb 11 '13 at 20:32
  • Have you tried cleaning the cache of your browser? By default the redirect is permanent and web browsers usually cache this information (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2). – cheesemacfly Feb 11 '13 at 20:43
  • yep. I even tried opening it up in a different browser with everything cleared out on a separate computer and still gets redirected with "/" It shouldn't be this difficult now should it? The file structure: /test/desktops/default.aspx Intended URL: /test/desktops I was able to achieve that URL with a file called /test/desktops.aspx but not with the subdirectory. – fseminario Feb 11 '13 at 21:15
  • What if you remove the conditions then (you can remove the whole `...`)? – cheesemacfly Feb 11 '13 at 21:26
  • if i remove the conditions, only the root pages work. none of the sub directories are found... – fseminario Feb 11 '13 at 21:29
  • I am really surprised. I tested this exact same rule and it always removes the trailing slash (event when trying to reach `/test/desktops/` it goes to `/test/desktops`). What if you try to see what's happening using the [Failed Request Tracing](http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules)? – cheesemacfly Feb 11 '13 at 21:39
  • I had it running already. I got the log and from the looks of it, /test/desktops runs fine until it gets to IIS and adds the slash {00000000-0000-0000-1D01-0080010000F9} Content-Type: text/html; charset=UTF-8 Location: http://xxx.xxx.com/test/desktops/ Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET any line in particular you would like me to look at? – fseminario Feb 11 '13 at 21:59
  • can you copy/paste the full log in a pastebin or equivalent? – cheesemacfly Feb 11 '13 at 22:03
  • In the trace, you are requesting `test/desktops` so it doesn't trigger the rule. The rule only removes the `/` at the end so if you request `test/desktops/` you will be redirected to `test/desktops`. – cheesemacfly Feb 11 '13 at 22:27
  • tried that too but still getting the slash on there updated http://pastebin.com/JZNjwRhS – fseminario Feb 11 '13 at 22:41
  • I see in your file the `CONDITION EVALUATION` returning `Succeeded false`. Can you try again without the conditions? – cheesemacfly Feb 12 '13 at 15:36
  • Then the problem comes from your IIS configuration. When you try to reach `test/desktops` does it work? If yes, please remove the conditions and provide the new full log when you try to reach `test/desktops/`. – cheesemacfly Feb 12 '13 at 16:50
  • i can't access /test/desktops/ or /test/desktops but i can /test/desktops/default.aspx. The default doc is set to default.aspx which i find strange. Updated Log with (/test/desktops/) and conditions removed: http://pastebin.com/jFP9Wfdq – fseminario Feb 12 '13 at 18:47
  • But you can change it by adding the section `` in your `web.config` file (http://www.iis.net/configreference/system.webserver/defaultdocument) – cheesemacfly Feb 12 '13 at 18:54
  • that's whats strange, the default for "/test/desktops/" was already set to default.aspx so /test/desktops/ should work... – fseminario Feb 13 '13 at 14:53
  • 1
    thanks cheese. I didn't know it needed to be a virtual directory not a physical path! DOH! – fseminario Feb 13 '13 at 21:59
1

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!

johnw182
  • 1,349
  • 1
  • 16
  • 22
0

The problem I had were links to PDF files having trailing forward slashes, so what worked for me in Windows Server 2008 R2 running IIS 6.1:

Click on the website that needs the rule and in the Features view open up URL Rewrite and then on the Actions section (right pane) choose Add Rule(s) and select Append or Remove the trailing slash symbol. In the next window on the drop down choose remove if exists.

Hope this helps.

Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
Onslot
  • 17
  • 5