CSharpFeeds - All your C# feeds in one place.

Sponsors

Friday, December 22, 2006

Remove "WWW" from URLs in ASP.NET

by Keyvan Nayyeri via Keyvan Nayyeri on 12/22/2006 8:16:07 PM

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.

email it!bookmark it!digg it!

Original Post: Remove "WWW" from URLs in ASP.NET

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us