如何知道一个网页链接是否被重定向到任何其他域或留在同一域


How to know whether a web link is redirecting to any other domain or staying at the same domain?

我想知道一个页面的状态是否页面是重定向到任何其他域或留在同一域。但这对我不起作用。

https://www.rebelmouse.com/rohit/612798926.html

当我打开这个url它重定向到其他域,我想检测它是否停留在rebelmouse.com或打开任何其他域。

我已经尝试了curl_init()来了解状态,但它给出了200,它没有提供特定的http代码来识别重定向。

代码:

function file_get_contents_curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       
        $data = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $urlInfo = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        curl_close($ch);
        echo $urlInfo;
        return $data;
    }
$ch = curl_init('url_here/');
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($code == 301) || ($code == 302)) { //301 and 302 specifies the redirects.
//your stuff 
}

在这里查看更多信息。也许你可以在这里找到你的答案