简体   繁体   中英

$_SERVER['HTTP_HOST'] not recognized

I have this simple statement that checks if the $_SERVER['HTTP_HOST'] returns the same value of the variable $C['ST__URL'] . If not it does a 301 redirect. Today we had a downtime because the $_SERVER['HTTP_HOST'] did not match with the $C['ST__URL'] variable.

The downtime was caused by a forced 301 redirect falling into a continuous loop, so the reason must be the $_SERVER['HTTP_HOST'] not been recognized.

The $C['ST__URL'] was correctly filled, so I am wondering which can be the cause of the downtime? An Apache temporary issue? A different configuration in the DNS zone?

$url = parse_url($C['ST__URL']);

    if($_SERVER['HTTP_HOST']!=$url['host']) {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: '.$C['ST__URL'].$_SERVER['REQUEST_URI']);
    }

I would first check in the apache request logs.

You say "the reason must be the $_SERVER['HTTP_HOST'] not been recognized. " but you don't really know that at all, do you? Why Not? You don't have any record of it. Now that you know this is an issue, set up to log it should it happen again:

if($_SERVER['HTTP_HOST']!=$url['host']) {

    error_log("Invoking redirect: server host $_SERVER['HTTP_HOST'] mismatch with url $url.\r\n"),3,"/var/log/http301redirect.log");

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: '.$C['ST__URL'].$_SERVER['REQUEST_URI']);
}

That is just a sample message, you would want to datestamp and customize the log message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM