<?php
/**
 * Redirect URL based on DNS TXT record value. Requires A records to be pointed to the server's IP Address and a TXT record with redirection value.
 * Author: Ryan Fitton (https://ryanfitton.co.uk)
 * Version: 1.0.0
 *
 * Examples:
 * example.com.         1800	IN	A	123.45.67.89
 * www.example.com.     1800	IN	A	123.45.67.89
 * example.com.         1800    IN	TXT	"redirect_https://www.google.com"
 * www.example.com.		1800	IN	TXT	"redirect_https://www.google.com"
 */

//The redirect prefix
$prefix = 'redirect_';

//Find the TXT DNS records for this host
$txts = dns_get_record($_SERVER['HTTP_HOST'], DNS_TXT);

//Loop through each TXT record
foreach($txts as $txt) {
    //Loop through each record entry
	foreach($txt['entries'] as $txtValue) {
        
        //If the text record includes 'redirect_'
        if (strpos($txtValue, $prefix) !== false) {
            
            //Explode the TXT value
            $explode_txtValue = explode($prefix, $txtValue);
            
            //Set the 1st key as the URL addrress and Sanitize data
            $url = filter_var($explode_txtValue[1], FILTER_SANITIZE_URL);
            
            //Check if URL is valid
            if (filter_var($url, FILTER_VALIDATE_URL)) {
                //Perform a temporary redirect (302)
                header('Location: ' . $url, true, 302);
                exit;
                
            //If the URL is not valid
            } else {
                //Display message on screen
                echo 'Invalid URL: ' . $url;
                exit;
            }
        }
    }
}
?>
