ERROR: HTTP/1.1 301 Moved Permanently Location

Hard Men, :smiley:

Check this weird thing out!
Fire-up your Wamp/Xampp and try this code:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ebay.com");
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://mydomain.com/$2$3', $result);
echo $result
?>

Now, instead of ebay.com loading you will see an error mssg like so:

[b]HTTP/1.1 301 Moved Permanently Location: http://www.ebay.com/[/b]

What does that mean ? On my Chrome, I don’t see ebay.com showing any likewise mssg. Nor, am I getting redirected to any other page.

I found the code here:

It is supposed to replace all relative links to absolute links.

Enjoy!

Try the PHP Manual for testing the results of an URL request.

http://php.net/manual/en/function.get-headers.php

This is what I learnt tonight at another place:
When you connect to the site, if it gives you a 301 header which is an indicator that the real address is something else. In this case:

when you see error at:

I can also add option to follow 30x redirects:
CURLOPT_FOLLOWLOCATION - follow HTTP 3xx redirects.
(The code is what I learn tonight and not about the 301 header issue as I already knew that from ages ago).

Ok. I can see an improvement with a suggested code. Check it out yourself. Although, all that header stuff echoed at the top of the screen looks unclean. Doesn’t it ? Getting rid of the echo.


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://ebay.com");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://mydomain.com/$2$3', $result);
echo $result
?>

Error 301 is something you have no control over. Only the person who owns that website does. It’s also not an error you should be worried about. Simply put, it means that the location is moved and where ever they redirected you to, that’s where it was moved to. It’s to tell web crawlers that the redirected place should be the new place the web crawlers should crawl. That’s it.


These errors don’t really pertain to PHP at all. It’s more of an HTTP related subject. Although PHP can set headers, I don’t think this particular issue has anything to do with PHP except the PHP code you posted.

I never should have opened this thread! Silly me!
Have a guess what I did!
Using cURL, I fetched:

(note: No “www”).
And faced that 301 error.
Opened Chrome naturally, to see if it shows the same error or not while I manually head-over to ebay. Now, instead of navigating manually over to:

I navigated to:
http://www.ebay.com
(Note the “www”).
I saw no error there and so got puzzled why cURL was showing the error! Therefore, opened this thread.
But now, i spotted my mistake!
This thread should be closed.
Thanks everyone.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.