IIS, Trailing Slashes, and HTML extension

I hate URLs that don’t end in something.  I do.  I REALLY do.  That’s why most of the website I have built end in .html or for the MVC ones, a trailing slash.  But don’t you just hate it when a search engine or user somehow picks up a URL that doesn’t have the trailing slash? e.g. /my-lovely/url/without-a-slash ARGH!  I see it a lot on WordPress sites because of the amount of sharing going on.  I would have thought this would have come up more often, but it seems people don’t mind it.  I mind.  I think I have trailing slash and .html OCD.  I found this post on stackoverflow which was great but as always it didn’t work – too many blogs saying it does, when it does.  So for posterity here is what I did:

<rule name="AddTrailingSlashRule1" stopProcessing="true">
<match url="(.*[^/])$" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{URL}" pattern="^/(.*)\.html$" negate="true" />
<action type="Redirect" url="{R:1}/" />

Bingo! Now all my URLs that come in without a trailing AND do not end in .html redirect properly and nicely.


Google will see http://www.myurl.com/cheese and http://www.myurl.com/cheese/ as 2 different URLs.

Leave a Reply

Your email address will not be published.