0121 31 45 374
Qoute Icon

IIS Url Rewrite appends trailing slash after page extension

Tim

I’m a big fan of IIS’s built in Url rewriting module, if you’re not using it, you should be to lowercase urls, set canonical urls and all sorts it’s great. One thing that caught me out when we started using it though was the fact that it would append a trailing slash (“/”) at the end of the url for *.aspx or *.php pages which was odd as I had IsFile set.

After a little digging and reading about what the IsFile and IsDirectory actually do, I found out that these actually check if the directory or file really exist on the disk –which wasn’t the case for the .aspx or *.php pages as they were dynamic pages.

To work around it, you need to change the format of the rule slightly and add a Regex pattern match to ignore urls that look like a file extension:

<rule name="Add Trailing Slash" stopProcessing="true">
    <match url=".*[^/]$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>

I hope that saves others a little hair pulling!

Liked this post? Got a suggestion? Leave a comment