Search code examples
networkingdnsrfccname

How to overcome root domain CNAME restrictions?


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?


Solution

  • 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}");
    ?>