Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Say I have mydomain.com:

Is it better to use option A:

[A] mydomain.com      123.45.67.89
[A] www.mydomain.com  123.45.67.89

or option B:

[A]     mydomain.com      123.45.67.89
[CNAME] www.mydomain.com  mydomain.com

What would be the impact actually by choosing one after the other?

Second question is, say I want to have subdomain at blog.mydomain.com. And I set it as an A records as follow:

[A] blog.mydomain.com  123.45.67.89

And to make it also accessible when user is typing www.blog.mydomain.com, I also insert the following record:

[A] www.blog.mydomain.com  123.45.67.89

Question is, is this a common practice? Or are there better ways to do this? What I want to achieve is (user access -> redirect to):

mydomain.com          -> mydomain.com
www.mydomain.com      -> mydomain.com
blog.mydomain.com     -> blog.mydomain.com
www.blog.mydomain.com -> blog.mydomain.com
other.mydomain.com    -> error

Cheers!

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

Generally the best practice, for ease of maintenance, would be something like this:

[A]     mydomain.com            123.45.67.89
[CNAME] www.mydomain.com        mydomain.com
[CNAME] blog.mydomain.com       mydomain.com
[CNAME] www.blog.mydomain.com   mydomain.com
[A]     mail.mydomain.com       123.45.67.89
[MX]    mydomain.com            mail.mydomain.com

This lets you change your A record for mydomain.com without disrupting your mail record or having to change each subdomain by hand.

You'll also need to set up your webserver to answer on those names. Here's an example of doing so in Apache:

NameVirtualHost 123.45.67.89:80
<VirtualHost 123.45.67.89:80>
    DocumentRoot /var/www/html/mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    ErrorLog logs/mydomain.com-error_log
    CustomLog logs/mydomain.com-access_log combined
</VirtualHost>

<VirtualHost 123.45.67.89:80>
    DocumentRoot /var/www/html/blog.mydomain.com
    ServerName blog.mydomain.com
    ServerAlias www.blog.mydomain.com
    ErrorLog logs/blog.mydomain.com-error_log
    CustomLog logs/blog.mydomain.com-access_log common
</VirtualHost>

For the redirects you mentioned:

Question is, is this a common practice? Or are there better ways to do this? What I want to achieve is (user access -> redirect to):

mydomain.com          -> mydomain.com
www.mydomain.com      -> mydomain.com
blog.mydomain.com     -> blog.mydomain.com
www.blog.mydomain.com -> blog.mydomain.com
other.mydomain.com    -> error

To actually change the URL that's displayed in a client's address bar, that would need to be done on server side with mod_rewrite, and is not a function of DNS.

share|improve this answer
    
Yes, this looks exactly like the new settings I'd like to apply (also suggested by @cheekaleak). What about the wildcard? In what situation is the wildcard needed? –  nightrod May 4 '11 at 19:42
    
Wildcard records aren't actually used all that commonly, but if you do set one up and a request doesn't match any of the names specified in your NameVirtualHosts it will then be served by the default document root you've configured in Apache. –  Bryan White May 4 '11 at 19:57
add comment

Option B. Use CNAME records to specify aliases for existing A records. Same applies to the blog & www.blog entries. Choosing the CNAME record allows you to change the actually IP in one location (the A record). If you make all entries as A records you would need to update each individually.

share|improve this answer
    
I get the idea. Makes perfect sense though as it's easier to manage. –  nightrod May 4 '11 at 19:29
add comment

In the first question, you can do it either way. The only real impact of having one as a CNAME is that when that name is looked up, it will cause a second DNS lookup in some cases to go fetch the name it's pointing to (so now I look up www.example.com and get a pointer to example.com and have to go look that up to actually get the IP). The difference is usually on the order of milliseconds, and sometimes the pointer's IP will be included in the response as well if the server knows about it, avoiding the second look up. It still creates larger DNS packets though. I prefer just defining them as A records and using CNAMEs for hosts outside my own domain.

As for the second question, you can configure them however you like, just make sure the web server will answer for them and provide the appropriate HTTP-level redirection as desired (remember, DNS won't redirect the browser's address request to another URL).

share|improve this answer
add comment

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.