如何使用 php 获取重定向的网址


How to get redirected url with php

>我正在尝试复制网站页面,但在浏览器上输入后它会重定向。

例如

我进入,http://www.domain.com/cat/121

它重定向,http://www.domain.com/cat/121/title-of-the-page/

当我尝试为"www.domain.com/cat/121"php复制函数时它不起作用...

如何获取重定向的新网址?

     $url='your url';
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirects
      curl_setopt($ch, CURLOPT_HEADER, false); // no needs to pass the headers to the data stream
      curl_setopt($ch, CURLOPT_NOBODY, true); // get the resource without a body
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // accept any server certificate
      curl_exec($ch);
      // get the last used URL
      $lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            curl_close($ch);
         echo $lasturl;

这将帮助您获取重定向的URL

使用标头函数重定向到特定 url。

header('Location: http://www.domain.com/cat/121');

如果您使用的是CMS,则可以使用适当的插件。

例如,对于Wordpress,您可以使用: https://wordpress.org/plugins/redirection/