Remove Trailing Slash From the URLs of Your ASP.NET Web Site With IIS 7 URL Rewrite Module

One of the aspect of SEO (Search Engine Optimization) is canonicalization. In this blog post, we will see how easy to work with IIS Rewrite Module in order to remove evil trailing slash from our URLs
2011-09-11 10:08
Tugberk Ugurlu


One of the aspect of SEO (Search Engine Optimization) is canonicalization. Canonicalization is the process of picking the best URL when there are several choices according to Matt Cutts, the head of Google’s Webspam team.

Here is how Matt Cutts explains what canonical URL is :

“Sorry that it’s a strange word; that’s what we call it around Google. Canonicalization is the process of picking the best URL when there are several choices, and it usually refers to home pages. For example, most people would consider these the same URLs:


But technically all of these URLs are different. A web server could return completely different content for all the URLs above.”

If you have multiple ways of reaching your web page (as above), then you need to sit down because it is time to make some decisions my friends.

Trailing Slash is Evil

Let’s assume that we have created a web application, an ASP.NET MVC app because we are so cool. We have our pretty URLs as well.

Let’s go to a page on our web site :



And another page :



We have got the same page content. As we have mentioned before, these two will be treated as two different web page and it will confuse the search engine a bit (even if they are so smart today).

The solution is pretty simple : when a page is requested with trailing slash, then make a 301 (permanent) redirect to the non-trailing-slash version.

IIS URL Rewrite Module

There are several ways of doing that with ASP.NET architecture :

  • You could write your own HttpModule to handle this.
  • You could do a poor man’s redirection on your controller (on your page load if the application is a web forms application).
  • You could use IIS URL Rewrite Module to easily handle this.
  • And so on…

In this quick blog post, I will show how we can implement this feature for our whole web site with IIS Rewrite Module.

URL Rewrite Module is an awesome extension to IIS. Installing it to your web server is also pretty easy if you haven’t got it yet. Just run the Web Platform Installer on your server, and make a search for “url rewrite”. Then the filtered result will appear and you will see if it is installed or not :


After you have it, you will see the management section inside your IIS Manager under IIS section :


Cut the crap and show me the code

Now, we are all set up and ready to implement this feature. As it is usual nearly for all Microsoft products, there are thousands (ok, not thousand but still) of way to approach this feature but the easiest way of implementing it is to write the logic inside your web.config file.

As you already know, there is a node called system.webServer under the root configuration node. IIS Rewrite Module reserves a node under system.webServer section and allow us to configure the settings there pretty easily. What we will do is to only write the following code under system.webServer node :

    <!--To always remove trailing slash from the URL-->
    <rule name="Remove trailing slash" stopProcessing="true">
      <match url="(.*)/$" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />

What this code does is to tell the module to remove the trailing slash from the url if there is one and make 301 permanent redirect to the new URL.

Don’t allow the code to freak you out. It might look complicated but there are good recourses out there to make you feel better. Here is one of them :

Using the URL Rewrite Module by Ruslan Yakushev

When you run your site after this implementation and navigate to /Home/About/, watch what is going to happen :



Isn’t that awesome? A little effort and perfectly clean way of implementing the 1 of a thousand parts of canonicalization.

Some Gotchas

  • In your development environment, if you run your web site under Visual Studio Development Sever, you won’t be able to see this feature working. You need to configure your application to run under at least IIS Express to see this feature working.
  • When you deploy your web site and see this feature not working on your server, it is highly possible that you misconfigured something on your server. One of the misconfiguration you might have done could be setting the overrideModeDefault attribute to Deny for rules under <sectionGroup name="rewrite"> inside your applicationHost.config file.
  • If you are on a shared hosting environment and you see this feature not working, then ask your provider if they have given you the permission of configuring this part.


by Scott on Friday, Nov 04 2011 18:07:55 +02:00

This is a great solution. Only downside is that it messes with VS debugging. BTW trailing slashes aren't evil....haveing two urls for the same content is evil. Which one you choose is mostly preferrence, but having a trailing slash appears to be much more common on the web then no trailing slash.

by tugberk on Friday, Nov 04 2011 21:00:43 +02:00


Yeah, you are right it is not evil. I was just teasing there.

With or w/o trailing slash is more like a philosophical argument just like with or w/o www argument. So, picking a way is the better one. I have chosen the w/o trailing slash approach because there is no point keeping something unnecessary at the end of url for me.

by timmytimeless on Friday, Nov 11 2011 03:46:47 +02:00

I am just doing an intuitive guess here, but isn't it so that a trailing slash indicates you landed on a directory page containing a collection of things like a listing of articles, while omitting the trailing slash means you are viewing a content page representing an individual "thing" such as an article.

shrimp recipes
by shrimp recipes on Sunday, Mar 18 2012 08:08:30 +02:00

From this blog i got the informaton regarding the removal trailing slash from the urls of which is very beneficial for me. I was searching for this for a long time.Thanks !

Tung Nguyen
by Tung Nguyen on Wednesday, Jun 03 2015 02:22:47 +00:00
Very simple solution. Thanks you for sharing this.
Jimmy Johanson
by Jimmy Johanson on Monday, Jul 20 2015 06:12:56 +00:00
So lucky for me when I meet your post. I found it a whole day :( Thanks.
by Pete on Monday, Oct 05 2015 14:21:44 +00:00
I tried this and it works well for all pages but the homepage - still leaves a trailing slash on
by raana on Tuesday, Nov 17 2015 18:20:12 +00:00
thx.its good
by Rolly on Saturday, Sep 03 2016 08:10:32 +00:00
Good to be here and to read this perfect article which you share wth us.