Excellent. It may be a challenge for me to incorporate this into the search-engine-friendly rewrite rules that Joomla uses, but I'll give it a spin on a staging box and see what happens.
What you need to do is check what's in the "Host" header, and if it
doesn't match the ServerName send a redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.foo\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.foo.com/$1 [L,R=301]
What kind of performance impact, in general, do simple rewrites like this impose on a busy server?