Basically visit http://www.example.com I would like it to stay there, that is working fine.
Basically visit http://bar.example.com it redirects to http://www..com, that is wrong
I want it to visit http://www.example.com because of the backreference within the RewriteCond


RewriteEngine On

RewriteCond %   !^$

RewriteCond %   !^www.(example).com [NC]

RewriteRule ^(.*)          http://www.%1.com/$1 [L,R]

Ubuntu 8.04
Package: apache2-mpm-prefork
Architecture: i386
Version: 2.2.8-1

Negated designs don't have any match and therefore you can't reference several that non-existing match.

But do this rule rather:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

Your problem doesn't match bar.mysite.com:

RewriteCond %{HTTP_HOST}   !^www.(mysite).com [NC]

You have to change it out to complement to be able to obtain the backreference working:

RewriteCond %{HTTP_HOST}   !^[^\.]+\.(mysite)\.com [NC]