Basecamp Login Email Marketing Make Payment
W3trends | Houston website design and
The W3 Blog: A Publication For Web Designers and Developers.
Our aim is to inspire and give back to the web design and development community.

About W3trends

W3trends is a Houston, Texas based company that specializes in web design, development and internet marketing.

Our team consists of talented individuals that love web design and developing new and exciting techniques and showing them off to the web.

For more about our services:

Web Design
Web Development 
Marketing and SEO


Stay in touch

Sign up for our newsletter

How to Configure URL Rewriting in IIS

Posted by Brad Gilbert on Tuesday, January 03, 2012 1:23 PM
URL Rewriting is becoming increasingly more popular and common in web development. It has many advantages over regular URLs: search engine crawlers prefer user friendly URLs because they can more easily categorize the content based on the URL, and users themselves like URLs that are easy to read.

By default ASP.NET and IIS7 does not have URL rewriting. There are many different ways and techniques that web development teams use to accomplish the same thing. Scott Guthrie's Blog Post Tip/Trick: Url Rewriting with ASP.NET does a good job rounding all the different techniques up.

In this How To I am going to cover basic IIS7 URL rewriting using the rewrite module. Understanding that you have basic web development knowledge, this should give you a good basis to get started and have your website rewriting your URLs. I had trouble finding good articles when trying to implement basic URL rewriting for my website.

The first step is to install the URL rewrite module for IIS7. Click Here to head to the Microsoft website to download this IIS7 extension. Installing is straightforward using the Web Platform Installer.

After that you will modify your web.config for your website to include the relevant information to rewrite your URLs. You can use IIS7 to automatically create the rules, but I did not find that this was reliable. For the most part the wizards did not accomplish what I wanted. After becoming frustrated with this, I went ahead and manually wrote the rules into the web config. As with many things in web development, The Regular Expression can get complicated, so I use this cheat sheet here.

The first step is to ensure that you remove trailing slashes as your URLs come in from users.

<system.webServer>
<rewrite>
<rules>
 
    <rule name="Remove trailing slash" stopProcessing="false">
        <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>
 
</rules>
</rewrite>

The next rule is to ensure that domain URL requests such as www.domain.com get redirected to www.domain.com/default.aspx.

<rule name="add default.aspx" stopProcessing="false">
    <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <match url="^$" ignoreCase="true" />
    <action type="Rewrite" url="default.aspx" />
</rule>

The last rule is the meat of the web.config section which removes the .aspx from being displaying in the URL.

<rule name="Rewrite page to aspx" stopProcessing="true">
    <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <match url="([a-z0-9-_]+)(.*)" ignoreCase="true"  />
    <action type="Rewrite" url="{R:1}.aspx{R:2}" />
</rule>

You have now successfully setup basic URL rewriting in IIS7. URLs such as www.domain.com/about.aspx will now show as www.domain.com/about.

For further reading and setting up rewriting involving query strings and different URL variations, here is a list of a few web development resources which helped me with those more complicated scenarios: