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?

JC Lee
  • 123
  • 1
  • 5

2 Answers2

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]
Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • 1
    I tried `RewriteRule ^/$ http://sub.domain.com? [R=301,L]` but it is not redirecting. – JC Lee Apr 02 '13 at 12:37
  • 1
    @komirad My mistake, it's in an htaccess file - use `^$` instead of `^/$`. – Shane Madden Apr 02 '13 at 20:43
  • It redirected but the query string on form submit didn't work. Some javascript widgets failed too. – JC Lee Apr 03 '13 at 02:33
  • @komirad Can you clarify whether you wanted to keep or remove the query string in the redirect? – Shane Madden Apr 03 '13 at 02:38
  • The query string need to be kept. Search form on sub.domain.com when submitted should go to domain.com/?q=dress – JC Lee Apr 03 '13 at 06:28
  • @komirad Remove the `?` from your configuration, then; as I mentioned in my answer, it's for if you want to remove the query string. – Shane Madden Apr 03 '13 at 15:51
  • There's a misunderstanding. The query string needs to be kept but it should not be appended to the redirected sub domain. With your solution, I got this **sub.domain.com?q=product**. Instead, I want **domain.com?q=product**. – JC Lee Apr 04 '13 at 16:22
  • @komirad Ok - so you want to keep the URL in the user's browser as entered, but send them the content from `sub.domain.com`? Is that correct? – Shane Madden Apr 05 '13 at 01:01
  • No. I only want to redirect the "home page" domain.com to sub.domain.com(or use rewriting if that's possible). Anything else such as domain.com/about or domain.com/?q=product should not be affected. Is that possible? – JC Lee Apr 05 '13 at 02:23
  • @komirad Ah, I understand now! See edit. – Shane Madden Apr 05 '13 at 04:11
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]
nn4l
  • 1,336
  • 5
  • 22
  • 40
  • 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. – JC Lee Apr 03 '13 at 02:36