I've wild card subdomains (*.domain.com) set on my server. Now i want to use .htaccess to redirect all *.domain.com demands to some script primary.php on my small server. I looked for code that will help accomplish the redirect however i haven't been entirely effective. The very best working code I've discovered is:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/%1 [QSA,R=301,L]

However, www.subdomain.domain.com is rerouted to domain.com/www.subdomain rather than domain.com/subdomain. How is this fixed within the code? It is possible to better method of carrying this out?

Thanks ahead of time!

Are you able to do this rewrite rule:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain\.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/%2 [R=301,L]