Home > OS >  Php Cname redirection
Php Cname redirection

Time:12-07

Recently I've been thinking about such a question: how to use php to recognize that a site is referenced with a CNAME record, and if so, redirect it to a specific document?

CodePudding user response:

$_SERVER['SERVER_NAME'] contains the domain name of the site being served, which you can plug into dns_get_record(), filtered to get only CNAME records. So, if the domain name that was used to view the site is a CNAME, the result will not be empty, which you can use as a conditional for your redirect. I can't think of any use case where this would be desired, but something like this:

if (!empty(dns_get_record($_SERVER['SERVER_NAME'], DNS_CNAME))) {
    header('Location: <whatever>');
    exit;
}
  • Related