www.example.com vs example.com [duplicate]

Possible Duplicate:
to www or not to www

Consider a website at www.example.com

When the URL is entered manually into Firefox's address bar as example.com , the browser automatically redirects to www.example.com.

Using Internet Explorer, the browser redirects to a default search engine with the search term for example.com.

Sites like codinghorror.com or example.net all are automatically changed to redirect to their www address.

Is there a setting on the web server to change this behaviour?

Any other issues coming into play here?


Solution 1:

Generally, you set up your webserver so that one domain is canonical, and any aliases you want are redirected to it. If specify which webserver, someone will be along shortly to tell you how :)

With Apache, you might do something like this:

<VirtualHost *>
    #our canonical name
    ServerName www.example.com

    #other domains we want to respond to
    ServerAlias example.com

    RewriteEngine on

    #if host is specified and not our canonical one, redirect
    RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         http://www.example.com/$1 [L,R=permanent]

    ...

</VirtualHost>

Note that you must still define A or CNAME records for all the aliased domains.

Solution 2:

I'd recommend setting up one of the two names to be the canonical and unique way to access your web site and the other to redirect to it. Don't use CNAME, or JavaScript redirection. Use HTTP redirection.

Personally I redirect from www.example.com to example.com because I'm against www. The reason is quite simple: the fact that you are using a web browser and http already says that you are trying to access the web site at that location, no need to add more redundant information. Also, hr.example.com may be HR's web site, does that mean then that you have a www department at your company? and if not, then it should be www.hr.example.com, which looks rather bad.

Solution 3:

Look at the url above. stackoverflow.com doesn't go to www.stackoverflow.com. They are two separate dns entries. Take a look at your DNS host configuration and try to add an A record for xyz.com and that should fix the problem.