Assuming you are using apache and have access to edit code files, add the following into your .htaccess file in the root directory of your site, somewhere right after the “RewriteEngine On” line.
RewriteCond %{HTTP_HOST} ^a.com [NC]
RewriteRule ^(.*)$ http://b.com/$1 [L,R=301,NC]