Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

This question already has an answer here:

I have created a website for a client (my first freelance job!) and my client is generally happy, however they want the address bar to always show the www. portion, even if it isn't typed.

Is this possible? I have purchased the domain through 123-reg and am hosting the website on Azure (as an Azure Website). I am guessing I need to add an A record to the DNS settings, would this be correct? Or is it a setting I need to change on Azure? Or something on the web.config file?

The site has been created with ASP.Net MVC4.

Thanks

share|improve this question
1  
    
@TildalWave's link there should solve the problem for you. But on a side-note, you might want to point out to your client that using the non-www version of the URL is better for two reason. 1) More efficient (you're not passing an extra 4 characters for every URL - who cares, I know). But from an SEO standpoint, those 4 characters will be used to display URLs in SERPs, so losing them means that you have 4 more characters for keywords. If the www is 301'd to the non-301, he can still use it when he wants to, it'll just make the site more efficient. –  nathangiesbrecht Feb 10 '13 at 22:14
add comment

marked as duplicate by DisgruntledGoat, John Conde Feb 12 '13 at 12:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

Redirect non-www to www in MCV4

Edit global.asax and add:

protected void Application_BeginRequest (object sender, EventArgs e)
{
   if (!Request.Url.Host.StartsWith ("www") && !Request.Url.IsLoopback)
   {
      UriBuilder builder = new UriBuilder (Request.Url);
      builder.Host = "www." + Request.Url.Host;
      Response.StatusCode = 301;
      Response.AddHeader ("Location", builder.ToString ());
      Response.End ();
   }
}
share|improve this answer
add comment

If you cannot change your Apache configuration, you can try http://wwwizer.com (top of the page) where you point the NAKED domain to the IP address of WWWizer, and your WWW subdomain to your server. This will redirect all requests to the WWW subdomain.

share|improve this answer
    
adds an additional lookup which ultimately adds to page load time. –  bybe Feb 10 '13 at 22:37
    
@bybe - Yes, however it is negligible (hosted by Amazon as well) and is probably even shorter than having your own server process that. I mean, both options are great, but it depends on what is easiest/best for the admin. –  ionFish Feb 11 '13 at 3:17
add comment

Yes, you can redirect urls without the www to then include them. How to do so depends on your host. Some hosts have an option for this and you can just press a button to make it happen.

Most host serve both www and no-www sites from the same directory on an Apache server. In this case you will need to use rewrite rules to do it.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.