Force WWW & Fix Redundant Hostnames on Google / SEO

I feel the term “SEO” is completely overused, however, there are a few things you want to do besides just having great content. One is make sure your site url is consistent. chrisbitting.com is different from http://www.chrisbitting.com. 

Google Analytics will provide you a suggestion to fix this if you’re experiencing traffic from multiple hostnames. Something like:

Property http://www.yourdomain.com is receiving data from redundant hostnames. Some of the redundant hostnames are:

This is easy to fix using your Global.asax page. Just add this to your code, replacing “yourdomain” with your actual domain. The Application_BeginRequest will catch and redirect to the altered url, also issuing a 301 to help search engines.

void Application_BeginRequest(object sender, EventArgs e)
    {
        

        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(

            "http://yourdomain.com"))
        {

            HttpContext.Current.Response.Status = "301 Moved Permanently";

            HttpContext.Current.Response.AddHeader("Location",

                HttpContext.Current.Request.Url.AbsoluteUri.ToString().ToLower().Replace(

                    "http://yourdomain.com", "http://www.yourdomain.com"));

            HttpContext.Current.Response.End();
        }
    }

You could do this using web.config + rewrite, but I enjoy this method more.

Force WWW & Fix Redundant Hostnames on Google / SEO

Error 500.19 with IIS / rewrite in web.config

500-19If you’ve added a rewrite to you web.config but are now receiving a HTTP Error 500.19 Internal Server Error, chances are you need to install the URL Rewrite Module. You might have assumed this is part of your install but it might be missing.

The install only takes a few seconds.

 

  1. Visit http://www.iis.net/downloads/microsoft/url-rewrite
  2. At the bottom of the page you can download the installer without using Web Platform.
  3. Run the MSI installer to add the URL Rewrite Module to your server. In my experience, I have not needed to reboot or reset IIS after installing this.
  4. Now you should successfully be able to use <rewrite>.

rewrite-mod

Error 500.19 with IIS / rewrite in web.config