Best practice for placing DNS records and subdomains

5,546

Solution 1

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.

Solution 2

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.

Solution 3

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:
5,546

Related videos on Youtube

nightrod
Author by

nightrod

Updated on September 18, 2022

Comments

  • nightrod
    nightrod almost 2 years

    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!

  • nightrod
    nightrod about 13 years
    I get the idea. Makes perfect sense though as it's easier to manage.
  • nightrod
    nightrod about 13 years
    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?
  • Bryan White
    Bryan White about 13 years
    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.