In one of my websites there was a complicated mechanism for redirecting mobile users from desktop pages to their mobile versions. When I needed to get the mobile final url in order to use it in my rel=”alternate” links I used a starting url and followed the redirects until getting the final url.
Here is the code I used:
1 2 3 4 5 6 7 8 9 10 |
$url="http://www.mysite.com/url"; stream_context_set_default(array( 'http' => array( 'method' => 'HEAD' ) )); $headers = get_headers($url, 1); if ($headers !== false && isset($headers['Location'])) { $finalurl=is_array($headers['Location']) ? array_pop($headers['Location']) : $headers['Location']; } |