我需要在PHP中获取重定向页面的URL。
假设我有一个类似http://example.com
的网址,当我打开它时,它会重定向到http://redirected_url.com
。所以这个PHP函数的输入和输出必须是这样的:
http://example.com
http://redirected_url.com
我该怎么做?
编辑:我不是指重定向。我需要重定向页面的网址。所以我不知道重定向的网址,但我需要找到它。
答案 0 :(得分:10)
很难从这个问题中得知,但如果你想告诉你正在访问的网址是否试图重定向你,你可以使用get_headers("http://some_url")
并检查响应的密钥[0]如果它发送了301状态代码。或者如果它具有如上所述的“位置”标题。 php docs
像这样:
function get_redirect_target($destination){
$headers = get_headers($destination, 1);
return $headers['Location'];
}
我会留下错误(和区分大小写)处理,但这应该让你到那里
答案 1 :(得分:-2)
header('Location: http://redirected_url.com');