Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Having used both methods in the past I just wandered which method is actually best practice when handling the www subdomain? Do I setup another A record in the DNS to point to the same IP ad the root domain or do I just stick to the root domain A record and then add a directive to the .htaccess file to redirect requests for www on to the root domain?

What are the reasons for your recommendations and are there pros and cons to SEO and how sites are viewed/indexed etc?

share|improve this question
1  
This question doesn't really make sense. Whenever you create a subdomain, you need to modify your DNS entry (either by changing an existing A record to a wildcard record or adding a separate record for that subdomain). How you want to point your subdomain or rewrite/redirect URLs on your web server is a separate issue. –  Lèse majesté Jul 22 '12 at 23:24
    
Admittedly my explanation isn't too clear on second glance. Basically I'm looking for best practices and generally it's best practice to setup a www subdomain even though it's not required. Then from other comments it seems best practice to then setup the htaccess redirect too to avoid the duplicate content issue in search engines. So it's part of the whole and specific to the www. subdomain, not any subdomain. –  Ian Jul 23 '12 at 10:05

2 Answers 2

I would add a DNS A record for www this way you are avoiding a 301 redirect that your server is handling. It's cleaner to do it at the DNS level whenever possible. If you're on a shared host 301's in htaccess are pretty common.

Don't forget in Google webmaster tools you can also tell them which you prefer www or non-www

share|improve this answer
1  
How you want to manage the www and non-www versions of the domain is a separate issue from your A records. No matter what you choose, you'll still need an A record for (or that includes) the subdomain. You set up your web server depending on how you want the domains to be redirected or not redirected. However, if you don't redirect one of them, you'll need to set a canonical URL for each page to avoid duplicate content penalties, and Google generally advises webmasters to redirect when possible. –  Lèse majesté Jul 22 '12 at 23:34
    
Thanks Anagio. I'd agree. Setting up the subdomain and also including the htaccess redirect seems to be the best overall solution to avoid duplicate content penalties. Good point about shared hosting too. In those cases they usually make the alias but don't handle the redirect. Thanks for the tip regarding Google Webmaster Tools too. –  Ian Jul 23 '12 at 10:09
    
Lèse majesté, thanks for the tips. I agree, a redirect is the best approach. Out of interest however, what do you mean setup a canonical URL for each page? You mean by setting the relationship attribute: rel="canonical"? –  Ian Jul 23 '12 at 10:12
    
@Ian: Yes, that's what I mean. Some webmasters don't have the ability, for technical reasons, to 301 redirect to their canonical URLs. So that's why Google developed the rel="canonical" link. –  Lèse majesté Jul 23 '12 at 10:50

Every host name that you want to be accessible on the web needs a DNS entry: either a CNAME or an A record (or AAAA if you have an IPv6 address). If you don't have one, all anyone who tries to load pages from that host will get is a DNS error message, something like "The server at nosuchhost.example.com cannot be found."

This applies just as well to www.yourdomain.com as to any other host name. The only exceptions are raw IP addresses like 127.0.0.1, and the fact that you can set up wildcard DNS records to map all hostnames under a given domain to the same address. (For example, Stack Exchange has a wildcard record for *.stackexchange.com, which matches both webmasters.stackexchange.com and any other hostname ending in .stackexchange.com.)

The actual question you seem to want to ask is, what should you do once you have working DNS records for both yourdomain.com and www.yourdomain.com. In general, there are three options:

  1. Serve the same content from both host names. This essentially creates a duplicate copy of your site, and could potentially cause issues with search engines. (In practice, however, most search engines will have ways of handling that particular situation gracefully, since it's so common.)

  2. Configure www.yourdomain.com to redirect the user to the corresponding page on yourdomain.com. This is the solution advocated by the folks at no-www.org, and, incidentally, what I prefer myself. One way to do this, if you're using Apache with mod_rewrite, is to add the following lines to an .htaccess file in the webserver root directory for www.yourdomain.com:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
    
  3. Configure yourdomain.com to redirect to www.yourdomain.com, as advocated by www.yes-www.org. This makes for longer and, arguably, less readable URLs, but could at least in theory have some advantages in certain rare situations, e.g. if you're running a large site that's both getting a lot of HTTP traffic and using yourdomain.com for some other purpose that needs its own dedicated set of servers. In practice, such situations rarely come up these days, and so the choice between options 2 and 3 is mostly a matter of taste.

Of course, you could also do something else, such as serve completely different content from yourdomain.com and www.yourdomain.com. However, that's likely to confuse your users, and so should generally be avoided.

share|improve this answer
1  
A common reason to use a www subdomain is so that your cookies aren't accessible by other subdomains--e.g. if you're serving static content from a cookie-less domain like static.example.com; otherwise, you'd need to use a separate SLD for your cookie-less domain to get the same performance benefit. –  Lèse majesté Jul 23 '12 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.