Portal Home > Knowledgebase > DNS > Forcing site onto WWW


Forcing site onto WWW





If you want users to always use www.yourdomain.com to connect to your website (rather than just yourdomain.com), you can add the following code to your global.asax:

    protected void  Application_BeginRequest( object  sender, EventArgs  e)
    {
        //check the request to make it starts with www
        //and is not localhost (dev)
        if  (!Request.Url.Host.StartsWith("www") && !Request.Url.IsLoopback)
        {
            //no ... redirect.
            UriBuilder  builder = new UriBuilder(Request.Url);
            builder.Host = "www."  + Request.Url.Host;
            Response.Redirect(builder.ToString(), true);
        }
    }

This code will run at the beginning of every request, check the Uri being used, and redirect to www.yourdomain.com.  So that you can even use this on your local machine for testing, it also checks to see if the code is being run locally (i.e. during development or debugging).



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read

Powered by WHMCompleteSolution