1

I have a PHP script call notfound.php that get executed when my web site hits a 404, and it will try to create a valid URL based on some logic that I have written. After I create a new URL I check if thats a valid URL or not.To do that I have tried get_headers, CURL, and file_get_contents. But only file_get_contents always return a valid answer. The problem I'm facing right now is if the new URL is also a 404, file_get_contents is again go in to my notfound.php, and take ages to say new URL is also a 404. I think its going on a loop or something.

Then I tried to send the $context to file_get_contents saying follow_location = 0, didnt work either.

I spend my whole day looking for answers but failed :(, any one got any ideas?

Thanu
  • 2,481
  • 8
  • 34
  • 53
  • 1
    Please take a look at this [answer to *How can one check to see if a remote file exists using PHP?* containing information about `file_get_contents()` and stream wrappers](http://stackoverflow.com/a/7456301/367456). You find additional comments there as well as other answers. Unless you share what exactly you've tried so far, your question is hard to answer. – hakre May 21 '13 at 12:07
  • Just seeing, in the [PHP HTTP stream context options](http://www.php.net/manual/en/context.http.php) there is no `follow_location` option. That probably already explains why you've got a problem with that. – hakre May 21 '13 at 16:15
  • That could be the case, And I find the answer you pointed out very interesting. For the moment I did what Hendriq suggested, passing a param in and check that is passed. This is working at the moment, so just dont wanna touch that again for sometime. I will try your suggestion later for sure since its look more cleaner. – Thanu May 21 '13 at 23:52

1 Answers1

1

I don't know why you would want to show valid content if there is none. That is bad for SEO, in my opinion.

So the best case what you could do is just send headers (404/500 etc) on the not found page.

On the page self you could show the user the message. That the url does not exists, but you recommend the following urls (give a couple of urls so the user can choose (always show the homepage)).

But if you want to stick with your design you chould set a get variable like ?no-lookup=true. SO if it hits the 404 page it wont end up in a loop. Then if it does no look up throw a 404 header and use cURL to get the header info and see if it is the 404 page

MKroeders
  • 7,562
  • 4
  • 24
  • 39