简体   繁体   English

我想跟踪 url 重定向并将它们作为链接回显

[英]I would like to trace url redirects and echo them as links

I would like to create a script that gets website redirects and echo the link except original link Something like this website我想创建一个脚本来获取网站重定向并回显除原始链接之外的链接 Something like this website

https://wheregoes.com/ https://wheregoes.com/

I tried this code but it doenst work, some urls have multiple redirects and I want to grab them all我试过这段代码,但它确实有效,有些网址有多个重定向,我想全部抓取

<?php 

$url="https://stvkr.com/v2/click-bOv2R-8X5JXO-14EJa-b6974c6b?tl=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Must be set to true so that PHP follows any "Location:" header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$a = curl_exec($ch); // $a will contain all headers

$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL

// Uncomment to see all headers
/*
echo "<pre>";
print_r($a);echo"<br>";
echo "</pre>";
*/

echo $url; // Voila
?>

The simplest way to create a 301 redirect with php is by using the php's header() function. Here is an example of how to use this function:使用 php 创建 301 重定向的最简单方法是使用 php 的 header() function。以下是如何使用此 function 的示例:

// Redirect to the specified page
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.example.com');

// Stop script execution
exit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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