"Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo." Brian Moore Apache DocumentationMod_rewrite will definitely do the trick. Fact is, mod_rewrite will do just about ANY trick. Use the right parameters, and the lil puppy can probably be trained to cook breakfast for you every morning. Trouble is, use the wrong parameters and your eggs just might be laced with cyanide.

The code csjavi posted is really close, with one potential problem and one definite problem. The potential problem is the forward slash before the regex parameter ($1). I "think" the parameter will already contain a leading slash, but I'd have to play with it to be sure. The definite problem is that, by default, mod_rewrite is going to perform a 302 Redirect, which isn't what you want. Use the [R] parameter to set it to a 301. Should look something like this (untested, so be wary):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteRule ^(.*)$
http://www.mydomain.com$1 [R=301,L]
And, Diane? These mod_rewrite commands
CAN be placed in your httpd.conf file, so you needn't give up that hope on that count.

In my opinion, however, mod_rewrite is over-kill for a job this simple. It carries too much server overhead and, frankly, I would always be a little worried about that cyanide.
Put this single line in the <VirtualHost> block for your www domain, and it will do pretty much the same thing as the mod_rewrite directives did:
RedirectMatch 301 (.*)
http://mydomain.com$1That RedirectMatch command, of course, is presupposing that you have configured httpd.conf with TWO separate <VirtualHost> blocks, one for domain.com and one for www.domain.com. That seems to be the way most people I've talked to recently are doing it. And, uh, in my opinion, it might just be the wrong way. Take a look at this simplified configuration:
<VirtualHost 217.7.165.70>
DocumentRoot /home/highrankings/www
ServerName highrankings.com
ServerAlias www.highrankings.com
</VirtualHost>
(This configuration assumes, of course, that DNS is properly configured and both domain.com and www.domain.com point at 217.7.165.70)
For the past few years, I was convinced that Google was smart enough to
automatically merge the PR for domain.com and www.domain.com, because that's what was happening on all my domains. I have only recently learned that others in this forum were configuring www differently than I have been doing, so I'm still testing, but can at least describe some of my preliminary results.
If you configure domain.com and www.domain.com in separate <VirtualHost> blocks, Google doesn't automatically merge backlinks. Ergo, you need to incorporate a 301 Redirect.
If you try to use extended versions of the
ServerAlias directive, backlinks again are not automatically merged. For example,
ServerAlias *.domain.com and
ServerAlias www.domain.com ww.domain.com wwww.domain.com both create aliases, but do not merge PR.
In every single instance I've tested, the simplified directive
ServerAlias www.domain.com has resulted in an IDENTICAL listing of backlinks for both domain.com and www.domain.com.
My tests have only been running for two weeks (since I discussed this with Compar in a thread here), and are definitely preliminary. I want to test a few other configurations, and it'll be a month or two before I can be confident these early results will hold true. In short, Caveat Emptor.

Hope this helps, Di.