Jump to content

Get URL after its been redirected


Pythondev

Recommended Posts

Ok,

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?

Thanks
Link to comment
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.
[code]
<?php

$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;
?>
[/code]
[url=http://php.net/curl]http://php.net/curl[/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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.