Jump to content


This topic is now archived and is closed to further replies.


Get URL after its been redirected

Recommended Posts


What I have is this... I have a link which when clicks redirects you to another page... For example lets say this link is example.com/website/hello/329472 and this redirects to domain.com/page.html

What I want to do is have a script where you type in the original url, in this case example.com/website/hello/329472 and it finds out what the page that it redirects to is...

This needs to be done with no user input though apart from typing in the original URL. and if possible id like it so it doesnt show the website loading or anything...

How can I do this?


Share this post

Link to post
Share on other sites
Note that the e in "curl_exec" is removed from the code below to allow the post to be accepted on the board.

$ch = curl_init();
$url = 'http://example.com/website/hello/329472';

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$r = curl_xec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close ($ch);

print $url;

If curl isn't installed you can do this manually using [url=http://ttp://www.php.net/fsockopen]fsockopen[/url] and complimentary functions by doing the HTTP request manually. There should be examples in the "User Contributed Notes" that you can look at.

You should also be able to use [url=http://www.php.net]fopen[/url] and [url=http://www.php.net/stream_get_meta_data]stream_get_meta_data[/url] to follow the redirects.

If this "redirect" is done internally by Apache using ModRewrite I don't know how you'd get the url. Perhaps by parsing a log file with ModRewrite debug info.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.