Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a URL redirects in PHP?

Tags:

url

php

rewrite

I saw someone ask a question about detecting if a URL redirects from groovy and perl but couldn't find anything on PHP.

Anyone know of somewhere I could find that code that does this?

like image 310
GeoffreyF67 Avatar asked Jan 09 '09 06:01

GeoffreyF67


People also ask

How do I know if a page is redirected PHP?

Create a Session variable when a user visits page1. php, then on page2. php check if that session variable exists. If so, then the user was redirected from page1.

How can I tell if a site has a 301 redirect?

You also need to make sure that people actually visit the HTTPS version of your site, which means using a 301 redirect between the HTTP and HTTPS version. To check that this redirect is in place, go to your homepage and look at the URL bar. You should see https://[www].yourwebsite.com/, plus a lock icon.

Which method takes a URL for redirection?

URL redirection, also known as URL forwarding, is a technique to give more than one URL address to a page, a form, or a whole Web site/application. HTTP has a special kind of response, called a HTTP redirect, for this operation.


2 Answers

$ch = curl_init('http://www.yahoo.com/');
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($code == 301) || ($code == 302)) {
  //This was a redirect
}
like image 55
Brian Fisher Avatar answered Sep 27 '22 16:09

Brian Fisher


Actually, I found this works best:

    function GetURL($URL)
    {
            $ch = curl_init($URL);

            curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);


            curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

            curl_exec($ch);

            $code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

            curl_close($ch);

            return $code;
    }
like image 24
GeoffreyF67 Avatar answered Sep 27 '22 16:09

GeoffreyF67