Keyvan Nayyeri

My daily musings about software and technology

Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0

It’s obvious that I’m trying to support the wave of NO-WWW! A long time ago I had written a HttpModule to remove “WWW.” from URLs. Last year I wrote a post with some topics for simpler URLs in web applications including an alternative solution to accomplish this task using ISAPI filters in Internet Information Services. Recently I also wrote another post about the issues related to SSL certificates and these extra characters in URLs.

One of the goals that I’m trying to accomplish is collecting all the possible ways to have WWWless URLs in the ASP.NET web applications, and I will keep writing such posts in the future as well. For now, I want to offer the simplest way to accomplish this in ASP.NET and IIS 7.0.

Today I spent some time switching from ISAPI filters to Microsoft URL Rewrite Module for IIS 7.0 and use it as an alternative for ISAPI tasks on Waegis and Nayyeri.NET. As you may know, this module is a very cool addition to the great new features in IIS 7.0 to complement it in one of the most common scenarios which is URL rewriting.

Beside the replacement of URL rewriting mechanism on Waegis (which was previously done with UrlRewriter.NET), I also removed Ionic ISAPI filter to implement Microsoft URL Rewrite module in order to redirect requests to URLs with “WWW.” prefix to the new URLs without it.

Here is the classic configuration of the rule necessary to remove “WWW.” from all the URLs on a domain with a permanent redirect to the new address.

Add a new rule to remove

The equivalent manual configuration is this:

<system.webServer>

  <rewrite>

    <rules>

      <rule name="Remove WWW" stopProcessing="true">

        <match url="^(.*)$" />

        <conditions>

          <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />

        conditions>

        <action type="Redirect" url="http://keyvan.ms{PATH_INFO}" redirectType="Permanent" />

      rule>

    rules>

  rewrite>

system.webServer>

But how does it work? First it checks for all the requests defined by the regular expression pattern in element that includes all the requests.

In the second step it checks the rules defined in section. If these conditions are met, and request has matched the pattern, then it applies the action defined in element. Here I defined my element as a pattern for HTTP_HOST parameter. When it matches the pattern (so it begins with “WWW.”), it passes the condition.

In the action I use a redirect task to the new URL which is a combination of my domain name and PATH_INFO parameter that yields my desire result. I also use the permanent redirection which is the recommended redirection type for this case.

8 Comments

James Alvin
Jan 25, 2009 1:19 PM
#

You should try http://www.codeplex.com/urlrewriter. The syntax for it can be written in a more universal method.

RewriteCond %{HTTPS} (on)?

RewriteCond %{HTTP:Host} ^(www\.)(.+)$ [NC]

RewriteRule (.*) http(?%1s)://%2$1 [R=301,L]

The above will take any domain HTTP or HTTPS and redirect to a NON-WWW domain. I like the mod_rewrite way because it doesn't suffer from the angle bracket bloat.


Sam
Dec 31, 2010 4:23 PM
#
Excellent! You know majority of posts out there is for move non www to www!

Ed Sansbury
Apr 27, 2011 12:14 PM
#
At last I got rid of the www for all pages on my Web site. Thanks Keyvan.

Brian
Nov 02, 2012 12:29 PM
#
Awesome! You just saved my day....and our team's as well. So a big thanks!

Sadegh
Jan 08, 2013 2:49 PM
#
What about post requests? I think it's better to ignore post requests from being redirected to prevent forms submitting related issues :)

indoor cycling
Apr 19, 2013 6:36 AM
#
I really love your website.. Very nice colors & theme.

Did you develop this website yourself? Please reply back as I'm hoping to create my own personal blog and would love to find out where you got this from or what the theme is named. Kudos!

Pradeep
May 17, 2013 9:24 AM
#
PLz list the advantage of no- WWW

More Info
Jun 02, 2013 11:25 AM
#
Good way of explaining, and pleasant piece of writing
to get facts regarding my presentation focus,
which i am going to convey in school.

Leave a Comment