to www or not to www [duplicate]

Solution 1:

Yes, it's best (SEO wise) to have one redirecting to the other. You want to avoid content duplication and link-juice fragmentation (although those 2 should be have been fixed by now on the major search engines).

<VirtualHost *:80>
    ServerAlias www.example.com
    RedirectMatch permanent ^/(.*) http://example.com/$1
</VirtualHost>

Keep in mind though that the no-www movement has the disadvantage that all subdomains will be also transfering the cookies of the main (no-www) domain.

Solution 2:

Certainly 301 one to the other.

It's necessary to make the www. the canonical version when you envisage you might at some point have other hostnames/subdomains under the same domain, with their own security contexts.

If you plump for the non-www. version, you won't be able to set cookies on the main example.com site without also making them available to anything on subdomain.example.com. If the subdomain is something like a userimg.example.com where you're trying to host user-submitted data with lower security rights, the non-www. version will be leaking cookies which the www. version would keep separate.

(Plus of course the other non-web-related stuff, like not being able to CNAME a bare domain.)

Solution 3:

There's no good reason not to provide both.