Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RedirectMatch 301 ^/sub/(.*)$ http://sub.domain.com/$1
<?php
$url = $_SERVER['REQUEST_URI'];
header('location: http://sub.domain.com'.$url);
die();
?>