Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to redirect only my top level domain eg. to 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: where it should have been

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
up vote 3 down vote accepted

Just below the RewriteEngine On, add:

RewriteRule ^$ [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 ^$ [R=301,L]

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

RewriteCond ${QUERY_STRING} ^$
RewriteRule ^$ [R=301,L]
share|improve this answer
I tried RewriteRule ^/$ [R=301,L] but it is not redirecting. – komirad Apr 2 '13 at 12:37
@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 when submitted should go to – 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


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.