开发者

Best way to get rid of www. and

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:

h开发者_StackOverflow中文版ttp://sub1.domain.com

http://sub2.domain.com

thx


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.


Why not use the ServerName directive ?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜