Of course you can. Take advantage of all the parameters offered by header()
:
<?php
if ($_SERVER['HTTP_HOST'] != 'domain.com'){
header("HTTP/1.1 301 Moved Permanently", true, 301);
header("Location: http://domain.com".$_SERVER['REQUEST_URI']);
}
The last parameter is forcing the response code to be 301, as you want.