Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
<?
$ref=$_SERVER['QUERY_STRING'];
if ($ref!='') $ref='?'.$ref;
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://newdomain.com/'.$ref)...