Apache Canonical Rewrite with Passenger (mod_rails)
Try replacing that last line by the following:
RewriteRule ^(.*) http://mydomain.com%{REQUEST_URI} [R=permanent,QSA,L]
If that still fails, I recommend to use a separate virtual host for the URL www.mydomain.com that just does a Rewrite to mydomain.com. Passenger doesn't play well with mod_rewrite.
Hope this helps.