-2

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?

  • Can you explain why? This may be better handled at the webserver level. If someone has pointed a CNAME at your server that you don't want, for example. – ceejayoz Dec 06 '21 at 18:49

1 Answers1

1

$_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;
}
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98