How to redirect only the root page to another subdomain via htaccess

.htaccess

I am trying to redirect only my top level domain eg. domain.com to sub.domain.com. The inner pages of the domain should not be redirected.

I followed the following link: How to redirect root and only root via htaccess?

But when doing a search on my site, the query was appended to the sub domain like:

sub.domain.com/?q=product where it should have been domain.com/?q=product

My current htaccess already has to following that has rewritten:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

What do I add to achieve the behaviour stated above?

Best Answer

Just below the RewriteEngine On, add:

RewriteRule ^$ http://sub.domain.com [R=301,L]

If I'm reading correctly, it sounds like you want to keep the query string. But if you want to delete it, you'd use:

RewriteRule ^$ http://sub.domain.com? [R=301,L]

Edit: To only redirect the / page in the case that it has no query string, use:

RewriteCond ${QUERY_STRING} ^$
RewriteRule ^$ http://sub.domain.com [R=301,L]
Related Topic