Redirect domain homepage to a subdomain

(Firstly I am not sure which area this post should go to)

I want to redirect only the main homepage of website www.example.com (and example.com) to new.example.com

but existing inner domains such as abc.example.com should come out as abc.example.com as well as example.com/123 should remain as example.com/123.

the site is made in joomla.

please help. i can’t find a solution.

Not sure if i was able to provide correct specification through my question. But I think you got the requirement correctly. the redirect to the new subdomain should be only when appearing from example.com or www.example.com. there should be no redirect when coming from other subdomains or subfolders. So do I need to list all the subdomains & subfolders in htaccess (or somewhere else)? if so how?

also the new.example.com has different content than www.example.com, so I am looking for redirect.

Thanks for the support!

Do you want it to be done with PHP? Otherwise it would be nice using .htaccess as far as I know. Your goal could be achieved through something like this.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^www?\\.example\\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\\.php?$ http://new.example.com [R=301,L]

:untested:

If the above does not work then better to post/ask in Apache Configuration forums.

Rajug,

Very nice! For what the OP asked for, you came very close (you only redirected the empty {REQUEST_URI})!

NS,

When I approach a redirection question like this, I verbalize a “specification” so I can think about the code which will be required to perform the redirection. Your specification appears to be:

Redirect example.com and www.example.com to new.example.com (is that using the same “main domain” or is that just another subdomain?) EXCEPT when a subdomain was requested via {subdomain}.example.com OR example.com/{subdomain’s subdirectory}.

Without a list of the subdomains, the second half of that EXCEPT part of the specification is impossible to accommodate.

Regards,

DK

NS,

Okay, perhaps I was trying to be TOO polite in saying that you need to provide more information so let me just go with NO subdirectories being redirected: Redirect example.com and www.example.com to new.example.com EXCEPT when a subdomain was requested via {subdomain}.example.com OR when a subdirectory has been requested. Quite simply, that’s:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\\.)?example\\.com^ [NC]
RewriteCond %{REQUEST_URI} ^([^/]+)$
RewriteRule .? http://new.example.com%{REQUEST_URI} [R=301,L]

The first RewriteCond matches www.example.com or example.com (no case), the second ensures that the request is to the domain’s document root and the RewriteRule redirects EVERYTHING to the new domain.

The message is that it’s all in creating a specification which completely defines what you’re trying to do with your redirection.

Regards,

DK