: Best practice for adding the www subdomain: Via an A record or htaccess redirect? Having used both methods in the past I just wandered which method is actually best practice when handling the
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?
More posts by @Steve110
2 Comments
Sorted by latest first Latest Oldest Best
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 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 yourdomain.com. In general, there are three options:
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.)
Configure 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 yourdomain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ %1/ [R=301,L]
Configure yourdomain.com to redirect to yourdomain.com, as advocated by 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 yourdomain.com. However, that's likely to confuse your users, and so should generally be avoided.
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
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.