I'm Keyvan Nayyeri, a 25 years old Ph.D. student at
the Computer Science department of
the University of Texas at San Antonio.
I'm also
a Software Architect and Developer and previously held a B.Sc.
degree in Applied Mathematics.
This is my blog where I publish content about various topics specifically Programming Languages and Compilers, Software
Engineering and Programming.
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.
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://nayyeri.net{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 <match /> element that includes all the requests.
In the second step it checks the rules defined in <conditions /> section. If these conditions are met, and request has matched the pattern, then it applies the action defined in <action /> 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.
Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0
Jan 23, 2009 4:54 PM
#
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0 : Keyvan Nayyeri
Jan 24, 2009 2:54 AM
#
Thank you for submitting this cool story - Trackback from DotNetShoutout
Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0
Jan 24, 2009 7:08 AM
#
Take a look…Keyvan posted a very nice article about removing the WWW
Dew Drop - Weekend Edition - January 24-25, 2009 | Alvin Ashcraft's Morning Dew
Jan 24, 2009 10:03 PM
#
Pingback from Dew Drop - Weekend Edition - January 24-25, 2009 | Alvin Ashcraft's Morning Dew
Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0
Jan 25, 2009 1:03 PM
#
DotNetBurner.com - hot .net content! DotNetBurner
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.
lillbra » Blog Archive » links for 2009-02-03
Feb 03, 2009 7:17 PM
#
Pingback from lillbra » Blog Archive » links for 2009-02-03
Leave a Comment