Remove "WWW" from URLs in ASP.NET
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.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
16 Comments : 12.22.06
Feedbacks
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.
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 :-)
In my opinion the Nth rule of simplicity would be the simplicity of the internet URLs. One of things that
#1
DotNetKicks.com
12.22.2006 @ 1:18 PM