简体   繁体   中英

Which approach I should use to direct example.com to www.example.com - redirect .htaccess or DNS record?

I have found a lot of information how to redirect example.com to www.example.com . Two methods DNS and .htaccess . The problem is that most answers do not provide hosting service type and seems different people recommend different option. I know .htaccess is no brainer for Shared Hosting.

Does anybody know which method is better for VPS ?

In order of least to most advisable places to issue an HTTP 30X:

  1. DNS
    • Well, it's impossible to perform an HTTP redirect with DNS, so this is right out.
  2. Application [eg: in PHP]
    • You can do it here, but really you want to do as little host-based muckery in the application, mostly of the sake of separation of concerns .
  3. .htaccess
  4. Server/Vhost Config
    • Yes, this is where you should do it. Rewrites and redirects are parsed once at server start, and handled by highly-efficient code.

Very Late Edit:
I'd like to add that performing redirects in the application code is not necessarily a bad thing, but for simple www. and other host/infrastructure-level redirects the simplicity can't be beat.

eg: foo.com to www.foo.com and :80 to :443

However , if you're also doing application redirects you need to have a bulletproof division between what redirects are handled by the the server and which are handled by the application. Otherwise you can descend into a circle of hell where the application and server disagree on how to redirect.

If that division is anything less than absolute then you're probably better off simply letting the application manage redirects entirely.

These types of suggest questions are frowned upon, but, the domain registry should handle both WWW and non-WWW domain names. Then at your server, you can choose to respond to both, or 301 redirect one to another via htaccess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM