Exactly one year ago this website saw the light of day and in that time the archive has grown to 201 posts about C#, ASP.NET and a little bit of other related stuff. This will continue in the year to come. Because I’m in a birthday mood today, here are a couple of offers for you.

Free flaming

We’ve had many great discussions through the comments and by email and I appreciate it a lot. Thank you for that. However, it was all specific to the individual posts and not general about the website or me. So, I’m asking you to tell me why I suck or even better, what I could do to suck less.

Nothing will be taken personally, so this is your free chance to tell me exactly how you feel about the .NET slave for everybody to see. Don’t hold back, bring it!

Wish a song

Think about a radio show where the listener calls in and ask the DJ to play a specific song. Regardless of the terrible analogy, I hereby invite you to ask me to write about any topic you find interesting. It could be a piece of code you want me to write or other developer related topics.

The only rule is that is has to adhere to my coding and writing principles, which basically will be my own problem. I’ll try to accommodate all the suggestions if possible. Send me a mail or a comment detailing your suggestion and remember that no suggestions are stupid.

So there you have it.

Many websites have two points of entry for each individual page – one with www and one without. It means that if other websites link to yours, they might link to the same page using two different addresses. If you make sure that all incoming links point to the same address, you also make sure that various page ranking algorithms of the different search engines give the page full credit for each incoming link.

We can achieve this be simply remove the “www” in the URL’s on a website. That also makes the URL shorter and maybe easier recognizable. There are two ways to do this; we can use server-side or client-side techniques. The server-side is the best one, but it cannot be used by all, so I provide a JavaScript version as well.

Client-side

Add this script tag to the header of your web pages. This is the solution I have implemented here on this blog, because I cannot use the server-side approach.

<script type="text/javascript">
var regex = new RegExp("(http|https)://www\.");
var url = location.href;
if (url.match(regex))
{
 url = url.replace(regex, location.protocol + "//");
 location.replace(url);
}
</script>

The reason why I can’t use the server-side approach is because dasBlog is a .NET 1.1 project and I haven’t got Visual Studio 2003 installed on my machine. In with the new, out with the old and face the consequences.

Server-side

The server-side version uses an HttpModule to filter the incoming requests and redirects them to the URL without “www”. I’ve modified a little upon the excellent HttpModule of Keyvan Nayyeri.

private static Regex _Regex = new Regex("(http|https)://www\\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);

private void context_BeginRequest(object sender, EventArgs e)
{
 HttpContext context = (sender as HttpApplication).Context;
 if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
 {
  string url = context.Request.RawUrl;

  if (_Regex.IsMatch(url))
  {
   url = _Regex.Replace(url, "$1://");
   context.Response.Redirect(url);
  }
 }
}

For discussions about the use of 302 vs. 301 redirections, please see the comments on Keyvan’s post for details and make your own decision.

Implementation

Download the RemoveWwwModule.cs below and put it the App_Code folder in the root of your website. Then add the following lines to the web.config’s <system.web> section:

<httpModules>
  <add type="RemoveWwwModule" name="RemoveWwwModule"/>
</httpModules>

Download

RemoveWwwModule.zip (,66 KB)