ASP.NET IIS URL Rewrite Module 2.0 for SEO-Friendly Extensionless URLs

This article focuses on how to use the IIS URL Rewrite Module 2.0, both with the IIS manager GUI interface, and / or editing the web.config to map SEO-friendly extensionless URLs to a single ASP.NET Page

Not long ago I published this article   about creating an extensionless "Short url" site using the URL Rewrite Module 2.0  , using the DB provider - which looks up an extensionless URL in a SQL Server database that you configure, and redirects the request to the final url that it is mapped to from your URLREWRITE SQL Server table.

In this demo, you will be able to have a url such as:

http://mysite.com/corp/FL/company-name-inc

and get it rewritten to a url similar to the following:

http://mysite.com/lookup.aspx?state=FL&companyname=company-name-inc

Search engines aren't crazy about the last type of url with lots of querystring parameters. However, they love the first kind because it provides indexable keyword content and context right in the url. So a keyword-rich url that is comprised for example of the entire title of an article and which is devoid of querystring items and meaningless id numbers or GUIDs is preferable from an SEO perspective.

That's why one of the Google search operators is "inurl:". Google (and other search engines) extract relevant keywords right out of the url itself, before they even index the content on the page.

Once the module is installed, it appears as a menu item in IIS manager under each web site and each application under that web site. So on my local machine, if I navigate in the IIS Manager left-side navigation pane to <MachineName>\Sites\Default Web Site\IIS7UrlRewriting, which is the application name of this demo, I will see the URL Rewrite UI:


By clicking the "Add Rule" link on the right panel, you are presented with the choices. You are welcome to discover the different ones, but for this demo I'm going to use the "Blank rule" template.



You can see that there are options for a rule name, how to match the url, conditions, server variables, Action Type, and the RewriteUrl.


For this demo I'm going to match on the pattern /corp/state/company-name, with no extension, as in the above sample.



The Regex for this is:

^corp/([a-z]+)/([_0-9a-z-]+)

and it will be rewritten to:

Default.aspx?st={R:1}&name={R:2}

This translates to a path of /corp/stateabbrev/company-name where R:1 is the first capture (the state) in a url matching the pattern, and R:2 is the second capture (the company name). You could also flesh out this REGEX with apostrophes, periods and any other character you would expect to match on.

The /corp/ "folder" in the url tells me that this is a company search ( I could also have a /people/ folder for "people" searches that would rewrite to a different utility page.)

The first REGEX expression matches on anything with letters in it (like a state name or state abbreviation), and the REGEX is marked "ignore case" in the IIS UI.

The second REGEX expression matches on any string containing an underscore, any number or letter, or a hyphen. This is designed to match on a company name like "John-Jones-Company-Inc".  You can test your rules right in the IIS manager UI:



So now in my Default.aspx page all I need to do is get the Querystring values for "st" and "name" and I have everything I need to go to the database, look up the company,and display custom content.

My site could have dozens or even hundreds of these SEO - friendly Urls, and each would map to the same page but with different items on the querystring.

To the search engines, each of these "pages" would look like a completely different page and URL - and have different content in it. The crawler would dutifully index each page.

You complete and test these rules right in the UI, and when you click Apply, they are saved into your web.config for that application.

The actual section it writes to the web.config looks like this:

<system.webServer>
         <rewrite>
            <rules>
                 <rule name="CompanyName">
                     <match url="^corp/([a-z]+)/([_0-9a-z-]+)" />
                     <action type="Rewrite" url="Default.aspx?st={R:1}&name={R:2}" />
                 </rule>
            </rules>
        </rewrite>
    </system.webServer>

It is easy to see that at this point it becomes quite easy to simply write these rules "by hand" without using the IIS GUI at all.


For this demo, I've simply rigged up the Default.aspx page to display what was sent to it in the rewritten url.

You can download the working demo Visual Studio Solution here. Be advised that this works with IIS - so you cannot use the built-in Web Development server, it's not smart enough to handle url rewrites. And of course, you'll need to install the IIS Rewrite Module first.

By Peter Bromberg   Popularity  (4450 Views)