Solution:
Adding some conditions to the standard WP rule-set it’s all that is usually necessary. In the current block at the end of the file in the question, it would be something like this:
# BEGIN WordPress
...
RewriteRule ^index\.php$ - [L]
# Add next lines:
RewriteCond %{HTTP_HOST} !^img\.mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !/wp-content/uploads/ [NC]
...
# END WordPress
In this case, there are several rules at the top of the file, before the # BEGIN W3TC CDN
comment, that could also be applied to the request as only some of them make sure it is to http://www.mydomain.com
, considering the conditions are only valid for the next rule.
So I think it needs at least another exclusion here:
# Add next line:
RewriteCond %{HTTP_HOST} !^img\.mydomain\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Maybe it is necessary to add the same 2 lines to other rules before the # BEGIN W3TC CDN
comment, though. I don’t know, but this is the general idea.