I applied the following mod_rewrite rule in Apache2 to redirect from non www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^exa
That is because the root path is /, and you are appending whatever you get in RewriteRule (the first case works fine because it doesn't match the condition so no rewrite is performed).
You can try something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
# for the home page
RewriteRule ^/$ http://www.mydomain.com/ [R=301,L]
# for the rest of pages
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]