Force www (or no www)
People can link to your site using http://domain.com or http://www.domain.com. A search engine will not always treat both the same, as you can see for yourself - try doing a "site:yourdomain.com" search, then compare to "site:www.yourdomain.com".
We can force everyone to use the same domain using mod_rewrite. Whether you wish to use www or not is up to you.
This code should be placed in the htaccess file in the root of your domain, i.e. domain.com/.htaccess
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
</IfModule>
The IfModule wrapper ensures we only execute this code if the mod_rewrite module is enabled. See portability notes for more information.
Inside the IfModule, the first four lines are discussed and explained in Start Rewriting (part one). All we are doing is ensuring we are ready to rewrite.
We have one RewriteCond that checks the if the hostname is "domain.com" (rather than "www.domain.com") and sends a 301 redirect header to the www domain. We use the .* regex pattern to ensure we forward the path as well - if you request domain.com/dir/page.html, we want to redirect to www.domain.com/dir/page.html, not just www.domain.com.
If you do not want to use www.domain.com and your preferred domain is domain.com (without www), we can do that as follows:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
</IfModule>