I have a convoluted situation and I want to make sure I set up the redirects properly.
Several months ago, the company rebranded itself with a different name. When they did, they registered a corresponding domain name and pointed it to the existing site. So both www.originalcompanyname.com and www.newcompanyname.com both pointed to the company website.
Close but it doesn’t take care of the non-www’d domain.
RewriteEngine on
RewriteCond %(HTTP_HOST} originalcompanyname\.com$ [NC]
RewriteRule .? http://www.newcompanyname.com%{REQUEST_URI} [R=301,L]
Where the first line ensures that mod_rewrite is not in comment mode, the second line matches originalcompany.name (note the escaped dot) at the end of the {HTTP_HOST} (so it will match both the www’d version and non-www’d version) and the third line simply issues a permanent redirect (R=301) without altering the originally requested URI. Remove the www. in the redirection is you care to (many webmasters are now dropping the www’d domains … as I’ve done below).
If you need more info on mod_rewrite, have a look at http://dk.co.nz/seo as that page is a lengthy tutorial with a lot of sample code with explanations.