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.
As you see my site redirects all requests to any URL with "www." to a URL without it. This is the default behavior in Community Server, Subtext and some other web applications and I saw some sites that are doing this such as ASP Alliance and CSharpFeeds.
I wanted to write about this some weeks ago but couldn't find the address of a site which recommends this functionality on internet sites. First time I saw it on Scott Water's blog but that post is not accessible due to a URL Rewriting exception in Community Server. However, I could find it today.
However, I recommend this myself because it gives you shorter URLs and makes them unique. Google lets you to set the default URL type for your site in webmaster control panel for Site Maps and you can set it to index your sites with "www." or without it.
But if you want to implement this feature in ASP.NET and need some advice, I wrote a simple HTTPModule for this purpose which redirects all requests from URLs with "www." to shorter URLs without it. This HTTPModule supports both http and https protocols and you can deploy it to an existing ASP.NET 2.0 site easily.
Note that probably there are some other options to accomplish this such as a URL Rewriter module but I think a HTTPModule is simpler and easier to use. First time I saw this approach in Community Server source code.
I named my HTTPModule WWWLess. It uses a Regular Expression to check if URL contains "www." then removes it with some Regular Expression and string manipulations and redirects incoming request to new URL. Source code for WWWLess is as simple as what you see:
public class Redirector : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
private static Regex regex = new Regex("(http|https)://www\\.",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
bool hasWWW = regex.IsMatch(url.ToString());
if (hasWWW)
{
String newUrl = regex.Replace(url.ToString(),
String.Format("{0}://", url.Scheme));
application.Context.Response.Redirect(newUrl);
}
}
#endregion
}
Compiled binary is available in download package. To deploy this HTTPModule to an existing site, simply copy WWWLess.dll to bin folder and add following line to Web.Config under <httpModules /> element:
<system.web>
<httpModules>
<add name="WWWLess" type="WWWLess.Redirector, WWWLess"/>
</httpModules>
</system.web>
WWWLess 1.0 is available on my file gallery with source code.
DotNetKicks.com
Dec 22, 2006 1:18 PM
#
Dusty
Dec 22, 2006 5:19 PM
#
Keyvan Nayyeri
Dec 22, 2006 9:06 PM
#
vikram
Dec 22, 2006 9:33 PM
#
Keyvan Nayyeri
Dec 22, 2006 10:11 PM
#
Vikram,
This is your personal idea but I (and many other developers) like URLs without "www.".
As I wrote in my post, it has some benefits:
1- Shorter URLs: they're better, simpler and easier to remember.
2- Unique URLs: there will be two different URLs for an ASP.NET resource (with "www." and without). There may be some social networking sites that distinguish between these two URLs and break your link popularity.
Probably there are some other reasons. However, I like this ;-)
Thanks and merry your Christmas, too.
Sonu Kapoor
Dec 23, 2006 9:37 AM
#
Keyvan Nayyeri
Dec 23, 2006 10:37 AM
#
Clarke Scott
Jan 15, 2007 8:39 PM
#
Keyvan Nayyeri
Jan 15, 2007 9:29 PM
#
Clarke
Jan 16, 2007 1:42 AM
#
Keyvan Nayyeri
Jan 16, 2007 2:19 AM
#
You can test it with any site you like. Just compare pageranks for www.site.com and site.com. On the other hand if you go to your webmaster panel in Google SiteMaps, you can find an option to let Google remove or keep "www." for your site.
On the other hand, if you do a 301 redirect, pagerank won't move to domain.com, it will move to domain.com/default.aspx and this ties your site to ASP.NET technology unless you use another URL Rewriter engine to redirect www.domain.com to domain.com.
I haven't used this for an AJAXi homepage yet.
However, if you like to use it with 301, it's just two lines of code ;-) But IMO there is no difference.
Thanks for keeping this discussion :-)
Counterbalanced
Jan 26, 2007 2:59 AM
#
Keyvan Nayyeri
Jan 26, 2007 3:36 AM
#
Kyle West
Aug 09, 2007 11:36 PM
#
Keyvan Nayyeri
Oct 24, 2007 1:50 PM
#
In my opinion the Nth rule of simplicity would be the simplicity of the internet URLs. One of things that
Rizwan Shaikh
Nov 17, 2007 1:29 AM
#
SSL Certificates and WWW. Prefix on Domain Names
Jan 14, 2009 4:21 AM
#
Older readers can remember my post in the latest days of 2006 that showed how to implement a simple HttpModule to eliminate the “WWW.” prefix from domain names in ASP.NET applications to have unique and shorter URLs for all the pages on a site. Later
Remove WWW. Prefix from URLs with URL Rewrite Module for IIS 7.0
Jan 23, 2009 11:46 AM
#
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
Leave a Comment