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

 avatar
#1
DotNetKicks.com
12.22.2006 @ 1:18 PM
You've been kicked (a good thing) - Trackback from DotNetKicks.com
 avatar
#2
Dusty
12.22.2006 @ 5:19 PM
I think it would be better to do a 301 redirect (permanent) instead of the "response.redirect" which uses a 302 redirect (temporary). Response.Status = "301 Moved Permanently"; Response.AddHeader("Location",newUrl); That seems to be the preferred method for SEO purposes.
admin avatar
#3
Keyvan Nayyeri
12.22.2006 @ 9:06 PM
Dusty, No, I don't think. This way pagerank will be kept on www.domain.com and won't move to domain.com/default.aspx and this is better since Google gives same pagerank to domain.com and www.domain.com.
 avatar
#4
vikram
12.22.2006 @ 9:33 PM
everything is fine but I some how do not like my URL to be without that "www". I have already done a URL Rewrite module to rewrite the URL of the site. and can add this small utility over there easily, but feel that the URL looks better with those "WWW" :-) Marry Christmas
admin avatar
#5
Keyvan Nayyeri
12.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.

 avatar
#6
Sonu Kapoor
12.23.2006 @ 9:37 AM
There was already an HTTPModule available, I am using it for http://dotnetslackers.com as well: http://ewal.net/2004/04/14/a-url-redirecting-url-rewriting-httpmodule/
admin avatar
#7
Keyvan Nayyeri
12.23.2006 @ 10:37 AM
Sonu, Good link. I didn't know about that. But it's a general URL Rewriting module than a specific one for this purpose although it works to accomplish this as well.
 avatar
#8
Clarke Scott
01.15.2007 @ 8:39 PM
"No, I don't think. This way pagerank will be kept on www.domain.com and won't move to domain.com/default.aspx and this is better since Google gives same pagerank to domain.com and www.domain.com." If you use a 301 the PR from the www will be added to the PR of the non-www domain and thus a higher PR. This is his point :) It IS a better solution
admin avatar
#9
Keyvan Nayyeri
01.15.2007 @ 9:29 PM
No, not at all. You're on the wrong way. Google gives same pagerank to www.domain.com and domain.com and domain.com/default.aspx is completely different so the type of redirection doesn't matter at all. Even if you redirect using 301, Google moves it back. You can simply check out my site's pagerank: www.nayyeri.net and nayyeri.net have pagerank 6 and nayyeri.net/default.aspx has pagerank 5.
 avatar
#10
Clarke
01.16.2007 @ 1:42 AM
I have heard this from Matt Cutts himself this is correct so unless Google has changed something within the last month or so I think you will find I'm right. A bigger problem is that issoes the Response.Redirect() casues with ASP.Net Ajax update panel so for that reason alone I'd change to be a 301 :)
admin avatar
#11
Keyvan Nayyeri
01.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 :-)

 avatar
#12
Counterbalanced
01.26.2007 @ 2:59 AM
It is better to do a 301 (permanently moved) redirect. You want to accumulate all the PR and incoming links to a single location. It is possible for www.example.com and example.com to have different PRs, you want them combined for ranking purposes. (The pagerank *will* move, but it can take a few months!) If you are having problems with 'domain.com/default.aspx' ranking as well as 'domain.com', remove links to the default.aspx page and just refer to domain.com. This will clear out the undesired references from the search ranking. Thanks for the code, very nice.
admin avatar
#13
Keyvan Nayyeri
01.26.2007 @ 3:36 AM
Thanks for sharing your thoughts. I'm still not sure if this has any effect but will check it on one of sites. I set my domain status in Google SiteMaps control panel to have "www." or not and haven't seen any difference so far. For "/default.aspx", I removed all these references but I think external links to this page (from the past) still have their effect on this page. However, I'll check it again. Thanks :-)
 avatar
#14
Kyle West
08.09.2007 @ 11:36 PM
I changed the code just a bit to get rid of the default.aspx too. String newUrl = url.ToString().Replace("://www.","://").Replace("Default.aspx",""); application.Context.Response.Redirect(newUrl); Kyle
 avatar
#15
Keyvan Nayyeri
10.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

 avatar
#16
Rizwan Shaikh
11.17.2007 @ 1:29 AM
Thanxx allot buddy it really helped me thanks once again...!

Leave a Comment