views:

115

answers:

4

I want all traffic to go to:

http://www.domain.com

http://domain.com

|

V

http://main.domain.com

I can use .htaccess or maybe something in PHP, I'm using zend framework. Please note I also have:

http://sub1.domain.com

http://sub2.domain.com

thx

+1  A: 

Why not use the ServerName directive ?

Xavier Maillard
+6  A: 

This rules will preserve the complete uri

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(domain.com)$ [NC]
RewriteRule ^(.*)$ http://main.%1/$1 [R=301,L,QSA]

PS: I cannot think up how to make second line more portable without explicit domain specification. I will appreciate someone who can correct me.

zerkms
Needs mod_rewrite to work, but that's often enabled. These rules go into the `.htaccess` file.
Thorarin
@Thorarin: he definitely has it enabled, because of this quote: "requests appropriately to .main., keeping /any/path/info/". But yes, for anyone who will read it later - it is good clarification.
zerkms
Yes I have mod_rewrite so this should work well. thanks
Joshua
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*) http://%1/$1 [L,R=301,QSA]
SM
@SM: so? what did you mean?
zerkms
+1  A: 

The server-side solutions seem a little unnecessary when the Domain Name Server protocol is designed to handle redirects exactly like these. Path info and POST variables are preserved transparently.

If you have access to the DNS records for your website, it is best to use a CNAME record to redirect users to the proper domain name.

The CNAME points people looking for one domain (e.g. site.com) to another (e.g. main.site.com). This is all done without any work on your server.

Simply add two records to your CNAME records pointing:

domain.com => main.domain.com
www.domain.com => main.domain.com

If your current DNS provider does not provide this, try using ZoneEdit, who have worked very well for us in the past.

phsource
CNAMES will still send the browser header for domain.com, and www.domain.com, he wants these perm redirected to main.domain.com, which will affect search ranking splits.
Tracker1
+1  A: 

What you are trying to do is a functionality of web server. So let the webserver handle the request. So the most efficient way is using .htaccess redirect directives. Doing it using PHP is an inefficient way.

Chaitannya