Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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]
share|improve this answer
1  
I tried RewriteRule ^/$ http://sub.domain.com? [R=301,L] but it is not redirecting. –  komirad Apr 2 '13 at 12:37
1  
@komirad My mistake, it's in an htaccess file - use ^$ instead of ^/$. –  Shane Madden Apr 2 '13 at 20:43
    
It redirected but the query string on form submit didn't work. Some javascript widgets failed too. –  komirad Apr 3 '13 at 2:33
    
@komirad Can you clarify whether you wanted to keep or remove the query string in the redirect? –  Shane Madden Apr 3 '13 at 2:38
    
The query string need to be kept. Search form on sub.domain.com when submitted should go to domain.com/?q=dress –  komirad Apr 3 '13 at 6:28

I am using this statement:

ServerName mydomain.tld
RewriteEngine On
RewriteRule ^(.*) http://www.mydomain.tld [R=301,L]

You can also reuse all parameters by this rule:

RewriteRule ^(.*) http://www.mydomain.tld$1 [R=301,L]
share|improve this answer
    
Tried RewriteRule ^(.*) http://www.mydomain.tld$1 [R=301,L] but the query string is appended to the redirected sub domain instead of the top level domain. –  komirad Apr 3 '13 at 2:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.