Removing trailing slashes with URL Rewrite

Posted by on . Last Updated on . Tagged:dotnetdevopsseo

In the previous post, I explained why canonical urls are important and how to enforce them . In this article, I will show you how to have a canonical host url.

Why does adding or removing trailing slashes matter?

In essence, it matters for the same reasons as I described in my previous post. That is, search engines will see site.com/post1 as different content to site.com/post1/ so we need to correct this in order to get a bit of SEO benefit. In this article, I will be redirecting any URL that comes with a slash at the end to one without.

Adding or removing trailing slashes via URL Rewrite

As per the snippet in our previous posts, we are going to create an inbound rule (which lives under system.web > rewrite > rules in our web.config):

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

In the first line, we are defining the name of the rule which can be viewed inside of inetmgr (IIS Manager) saying that we want to stop at this rule and return the response to the client. Next up, we give our matching criteria which we have wildcarded to match all requests that end with a /. Then we have added a condition set which is definted to match all criteria inside. We use the server variable {REQUEST_FILENAME} to determine whether or not the path requested is a physical file or directory on disk. The last attribute in each condition is a negation, which is equivilent of saying don’t match a physical directory or file (we don’t want to screw the routing for those!).

Finally, we define the action that we wish to take if both the match and all the conditions in the conditions section have been satisfied. The action that we wish to take is to redirect the user to the page without the slash, so we use the request capture group {R:1} (more on this in a future article).

And that’s it, you should be redirecting people from say site.com/post1/ to site.com/post1.

Stuart Blackler is a seasoned technologist with over 15 years of commercial experience in the .NET ecosystem. Holding a degree in Computer Science, Stuart has earned certifications as a C# developer through Microsoft and as an AWS Solutions Architect and Developer. Stuart is the creator of the popular YouTube channel CodeWithStu, where he delves into topics close to his heart, including .NET, AWS, DevOps, and software architecture with a commitment to sharing knowledge and fostering a community of learners.