I have heard this thread (.htaccess redirect all pages to new domain) very informative. However, my request is a bit different from this original request.
Above thread talks about redirecting from old domain to a new domain, however, I have requested to only forward main domain to a subdomain.
Here is a question - How do I automatically forward all the request from my main domain to subdomain. For example -
Landing URL: http://example.com Forwarding URL: http://sub.example.com
Landing URL: http://example.com?var=foo Forwarding URL: http://sub.example.com?var=foo
If someone lands on my forwarding URL, I do not want them to be again redirected with 301. In simple words, I do not want to use a rule which always redirects all the request which lands on http://sub.example.com if the landing URL is the same.
If I follow the advice from the above link, I believe that will happen in my case as I want to forward from the main domain to a subdomain.
Here is what I have so far and it is doing partial job. It only forwards my one domain to another subdomain. However, it does not work when there are any URL parameters, it just stays the same old URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://sub.example.com/? [R=301,NE,NC,L]
In simple words - my code does not react when there is http://example.com?var=foo and it remains as it is.
Any guidance would be great.