If you have any idea how to do this for any other web server not listed here, let me know and I will place the information here.
Apache
These directions are for Apache and require that mod_rewrite be enabled.
To make all the traffic for http://example.com/ politely and silently redirect to http://www.example.com/ simply create a file called .htaccess (if it doesn’t already exist) and add the following to it, changing the red text to match your domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
For Apache, the line beginning with RewriteCond requires a backslash in front of each period in the domain name, but backslashes should not be used in the second line that begins with RewriteRule.
IIS 7+
To make all the traffic for http://example.com/ politely and silently redirect to http://www.example.com/ follow these steps:
First, install the URL Rewrite module.
Now add the following code to the <system.webServer> section of your web.config or applicationHost.config:
<rewrite>
<rules>
<rule name="Redirect example.com to www" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
(Thanks to Scott Forsyth’s Blog; visit him if you want a point-and-click GUI method of setting up your redirect.)
nginx
To make all the traffic for http://example.com/ politely and silently redirect to http://www.example.com/ simply add a new server entry to your nginx.conf file (or conf directory such as sites-available/sites-enabled on Debian systems) and add the following to it, changing the red text to match your domain:
server {
listen [::]:80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
In this case, your primary server block should not contain your naked domain.
Article at :yes-www.org