I do in the .htaccess file redirect from www to the main host in the following way:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
We must make sure that this rule does not apply to robots.txt, that is, there is no redirect to http://www.site.com/robots.txt