How to overcome root domain CNAME restrictions?

141,224

Solution 1

Thanks to both sipwiz and MrEvil. We developed a PHP script that will parse the URL that the user enters and paste www to the top of it. (e.g. if the customer enters kiragiannis.com, then it will redirect to www.kiragiannis.com). So our customer point their root (e.g. customer1.com to A record where our web redirector is) and then www CNAME to the real A record managed by us.

Below the code in case you are interested for future us.

<?php
$url = strtolower($_SERVER["HTTP_HOST"]);

if(strpos($url, "//") !== false) { // remove http://
  $url = substr($url, strpos($url, "//") + 2);
}

$urlPagePath = "";
if(strpos($url, "/") !== false) { // store post-domain page path to append later
  $urlPagePath = substr($url, strpos($url, "/"));
  $url = substr($url, 0, strpos($url,"/"));
}


$urlLast = substr($url, strrpos($url, "."));
$url = substr($url, 0, strrpos($url, "."));


if(strpos($url, ".") !== false) { // get rid of subdomain(s)
  $url = substr($url, strrpos($url, ".") + 1);
}


$url = "http://www." . $url . $urlLast . $urlPagePath;

header( "Location:{$url}");
?>

Solution 2

The reason this question still often arises is because, as you mentioned, somewhere somehow someone presumed as important wrote that the RFC states domain names without subdomain in front of them are not valid. If you read the RFC carefully, however, you'll find that this is not exactly what it says. In fact, RFC 1912 states:

Don't go overboard with CNAMEs. Use them when renaming hosts, but plan to get rid of them (and inform your users).

Some DNS hosts provide a way to get CNAME-like functionality at the zone apex (the root domain level, for the naked domain name) using a custom record type. Such records include, for example:

  • ALIAS at DNSimple
  • ANAME at DNS Made Easy
  • ANAME at easyDNS
  • CNAME at CloudFlare

For each provider, the setup is similar: point the ALIAS or ANAME entry for your apex domain to example.domain.com, just as you would with a CNAME record. Depending on the DNS provider, an empty or @ Name value identifies the zone apex.

ALIAS or ANAME or @ example.domain.com.

If your DNS provider does not support such a record-type, and you are unable to switch to one that does, you will need to use subdomain redirection, which is not that hard, depending on the protocol or server software that needs to do it.

I strongly disagree with the statement that it's done only by "amateur admins" or such ideas. It's a simple "What does the name and its service need to do?" deal, and then to adapt your DNS config to serve those wishes; If your main services are web and e-mail, I don' t see any VALID reason why dropping the CNAMEs for-good would be problematic. After all, who would prefer @subdomain.domain.org over @domain.org ? Who needs "www" if you're already set with the protocol itself? It's illogical to assume that use of a root-domainname would be invalid.

Solution 3

I don't know how they are getting away with it, or what negative side effects their may be, but I'm using Hover.com to host some of my domains, and recently setup the apex of my domain as a CNAME there. Their DNS editing tool did not complain at all, and my domain happily resolves via the CNAME assigned.

Here is what Dig shows me for this domain (actual domain obfuscated as mydomain.com):

; <<>> DiG 9.8.3-P1 <<>> mydomain.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2056
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;mydomain.com.          IN  A

;; ANSWER SECTION:
mydomain.com.       394 IN  CNAME   myapp.parseapp.com.
myapp.parseapp.com. 300 IN  CNAME   parseapp.com.
parseapp.com.       60  IN  A   54.243.93.102

Solution 4

You have to put a period at the end of the external domain so it doesn't think you mean customer1.mycompanydomain.com.localdomain;

So just change:

customer1.com IN CNAME customer1.mycompanydomain.com

To

customer1.com IN CNAME customer1.mycompanydomain.com.

Solution 5

Sipwiz is correct the only way to do this properly is the HTTP and DNS hybrid approach. My registrar is a re-seller for Tucows and they offer root domain forwarding as a free value added service.

If your domain is blah.com they will ask you where you would like the domain forwarded to, and you type in www.blah.com. They assign the A record to their apache server and automaticly add blah.com as a DNS vhost. The vhost responds with an HTTP 302 error redirecting them to the proper URL. It's simple to script/setup and can be handled by low end would otherwise be scrapped hardware.

Run the following command for an example: curl -v eclecticengineers.com

Share:
141,224
Geo
Author by

Geo

I have been exchanging knowledge for over 11 years, StackExchange just makes it easier. Come and find out which other SE sites I collaborate. Follow me on Twitter.

Updated on December 31, 2021

Comments

  • Geo
    Geo over 2 years

    We are hosting many web applications for our customers. As is obvious they want to use their own domains to refer to those applications, usually they want that any user that either type http://www.customer1.example or http://customer1.example goes to their web application.

    The situation we are facing is that we need to have the flexibility to change IP addresses in the near future. And we don't want to rely on the customer doing the A record change on their domains. So we thought that using CNAME records will work, but as we find out CNAME records will not work for the root domain.

    Basically:

    customer1.example IN CNAME customer1.mycompanydomain.example //this is invalid as the RFC
    www.customer1.example IN CNAME customer1.mycompanydomain.example //this is valid and will work
    

    We want to be able to change the IP address of customer1.mycompanydomain.example or the A record and our customers will follow this record which we have control over.

    in our DNS it will look like:

    customer1.mycompanydomain.example IN A 192.0.2.1
    

    Any ideas?

  • Rubix
    Rubix over 8 years
    This particular answer was very helpful to me since I wanted to point a root level domain at a CDN. Most CDNs are by necessity an FQDN, since it could resolve to different IPs at different locations or times. I use DNS Made Easy and was able to use the ANAME record type.
  • Matt Clark
    Matt Clark about 8 years
    This does not actually answer the question that 69k+ people have come to this thread looking for. The question is more about DNS and has nothing to do with PHP.
  • Ed Bishop
    Ed Bishop almost 8 years
    I couldn't agree more. Wanting to host a site from the 'naked' domain name is a common and logical thing to do. It uses less characters, it looks better etc. The url's own protocol identifier (www) is a vestigial part of the url if was even necessary in the first place (it wasn't).
  • Jacob Evans
    Jacob Evans over 7 years
    ANAME's are nice, or you can just 301 all non-www to www. via free 301 redirection service 198.251.86.133
  • SamTzu
    SamTzu about 6 years
    The question was about DNS. Not about PHP coding.
  • Patrick Mevzek
    Patrick Mevzek over 5 years
    Obfuscation, if really needed (the DNS is public....), should use RFC2606 guidance. And RFC5737 or 3849 for IP addresses
  • Patrick Mevzek
    Patrick Mevzek over 5 years
    HTTP_HOST is the hostname, like its name implies, not an URL. Hence there won't be any http:// to remove, nor / ($urlPagePath will always be empty). Cf httpd.apache.org/docs/2.4/expr.html. Due to the way the code tries to get rid of subdomain, it will also not work for things like www.example.co.uk where co.uk has to be considered as a whole. It also does not handle HTTPS. And finally using PHP just do a an HTTP redirection where any webserver can do it in configuration, is overcomplicated. So in short this certainly should not be the validated answer for this question.
  • BPDESILVA
    BPDESILVA over 5 years
    If this rule should apply to all hosts then it should be done as a apache config
  • Jussi Hirvi
    Jussi Hirvi over 4 years
    For me (BIND 9.8.2), if the records are for domain customer1.com, this works...but is interpreted as specifying CNAMe for a subdomain customer1.com.customer1.com. If I add a point to the first item, the record will be interpreted correctly, but it does not work anymore. I see no solution here.