Not_Lazy Not_Lazy - 4 months ago 10
PHP Question

PHP redirection based on DNS

say i have a file named redirections.txt that looks like this:

www: http://www.example.com/hub/
icloud: http://www.example.com/icloud/
dev: http://www.example.com/development/latest/projects.php


how would i go about processing that text document as
domain-prefix: redirect-url
? (or "if $_SERVER['HTTP_HOST'] equals domain-prefix.example.com, goto redirect-url")

currently i have:

$file = file('redirections.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($file as $line => $cont){
preg_match('/(*.?): http:\/\/www.example.com\/(*.?)\//', $cont, $matches);
print_r($matches); // Debug. Was trying to see if it worked.
}

Answer

You can use explode() to split the line:

$split = explode(': ', $cont);
if (count($split) == 2) {
    list ($domain_prefix, $redirect_url) = $split;
    if ($_SERVER['HTTP_HOST'] == "$domain_prefix.example.com") {
        header("Location: $redirect_url");
        exit();
    }
}
Comments