2

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?

0

2 Answers 2

4

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] 
10
  • 1
    I tried RewriteRule ^/$ http://sub.domain.com? [R=301,L] but it is not redirecting. Commented Apr 2, 2013 at 12:37
  • 1
    @komirad My mistake, it's in an htaccess file - use ^$ instead of ^/$. Commented Apr 2, 2013 at 20:43
  • It redirected but the query string on form submit didn't work. Some javascript widgets failed too. Commented Apr 3, 2013 at 2:33
  • @komirad Can you clarify whether you wanted to keep or remove the query string in the redirect? Commented Apr 3, 2013 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 Commented Apr 3, 2013 at 6:28
0

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] 
1
  • 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. Commented Apr 3, 2013 at 2:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.