Jump to content

CURL Redirect problem - "URL has moved here."?


joshj

Recommended Posts

Hello,

 

I had a redirect script working, but lately it has stopped and the page in the browser just has: "The URL has moved here." I'm not sure where this is coming from or why it isn't redirecting.

 

PHP Code:

$ch = curl_init();

$fp = fopen("redirecting.html", "w");

 

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_REFERER, $referer);

#curl_setopt($ch, CURLOPT_FILE, $fp);

curl_exec ($ch);

curl_close($ch);

fclose($fp);

 

I added the line curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); and it redirects fine but the original url is kept in the address bar which is not desirable.

 

It would be great if someone knew how to fix this.

 

Josh

what do you meant he original url is kept in the address bar? curl is for retrieving webpages, not redirecting. are you trying to redirect someone so when they visit:

http://yourhost.com/oldpage.php

it forwards them to:

http://yourhost.com/newpage.php

??????????

 

if so, the code for that is:

<?php
  header('Location: newpage.php');
  exit;
?>

Someone clicks the link: http://yourhost.com/oldpage.php

 

And they arrive at http://somewhere.com

 

curl_setopt($ch, CURLOPT_REFERER, $referer);

This sets the referrer value which is what I need.

 

Can I set the referrer value with

<?php
  header('Location: newpage.php');
  exit;
?>

?

The referrer needs to be oldpage.php?

 

Well, the curl method isn't actually sending them to that page. PHP fetches the page for them and displays the contents of it. If you want them to be forwarded with the right referrer, i think you have to use a JavaScript forward.

 

Why does somewhere.com need the referrer set?

The $url stored in my php script is different from the one that I get in the browser message (the url has moved here). I think the remote site is blocking my redirect some how. Not sure why or how, but I can do a simple java redirect or php header('Location: http://xxx..."); and using the line "curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);" works too.

 

Not sure what the difference is.

Can you share what the value of $url and $referer is? If I understand everything right, the page located at $url has moved to a new place, so you need to use the FOLLOWLOCATION or update the value of $url.

 

...and let me try to explain this further....

 

CURL is not a redirect. The client's browser is fetching your PHP page that has the CURL code in it. Your PHP code is then fetching the contents of $url and displaying it to the client's browser. The client's browser never makes it's own request to $url page. Therefore, the value in the location bar SHOULDN'T say the value of $url because their browser is in deed looking at your PHP script, not the $url page fetched in the background.

 

If you want the client's browser to actually go to that page, you can use a PHP's header(), HTML's meta-redirect, or JavaScript's window.location.href. But, all will have a referrer of the last page the client's browser was at. You cannot control what the user's referrer is.

 

Does that make more sense?

Archived

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

×
×
  • 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.