Page 1 of 1

How do you direct unknown pages to 404 ?

Posted: Sat Jul 13, 2013 3:16 am
by simonmlewis
We have a script that takes pages that don't exist to a custom error page - index.php?page=error.
But this is causing google issues as it is caching the dead pages because they just direct internally.

I want to design the error_docs pages, but I don't know how to point a page that doesn't exist, to the error pages?

Code: Select all

  function getPage()
  {
  $thispage="includes/".$_GET['page'].".inc";

  if (file_exists($thispage)) 
  {
   include $thispage;
  } 
  else 
  {
  echo "<meta http-equiv='Refresh' content='0 ;URL=/error'>";
  }
  } 
This is it at the moment. So how do you say "else { show 404 } " ??

Re: How do you direct unknown pages to 404 ?

Posted: Sat Jul 13, 2013 7:18 pm
by requinix
Send a

Code: Select all

header("HTTP/1.1 404 Not Found");
with your error page.

And try not to redirect to the error page. include() it

Code: Select all

$_GET["page"] = "error";
include "index.php";
or, IIRC, send the header as above and use an ErrorDocument 404 which will cause Apache to automatically "include" the error page for you.

Re: How do you direct unknown pages to 404 ?

Posted: Mon Jul 15, 2013 4:55 am
by clark551
header("Location: err.php?e=404");
you can also use this to direct your unknown pages to 404...

Re: How do you direct unknown pages to 404 ?

Posted: Mon Jul 15, 2013 10:05 am
by simonmlewis
It says not to redirect. So do I use a window.script to point to the error page, and within that, add the code you give for 'header' into it?
In other words - does it go from the page that doesn't exist to the error page to the 404 ??

Re: How do you direct unknown pages to 404 ?

Posted: Mon Jul 15, 2013 1:58 pm
by Christopher
I think you are getting confused my the word "page" here. The user requests a URL (which is called a page) and you return a HTML document (which is called a page) and one or more a PHP scripts may have generated that HTML "page". So a HTML document with a 404 HTTP header is the response to URLs for which the site does not have valid response for.

Re: How do you direct unknown pages to 404 ?

Posted: Mon Jul 15, 2013 2:02 pm
by simonmlewis
So do you "fake" the combined PHP page to make it appear as a 404 ?

I'm still lost as to what the final code shoudl be.

Re: How do you direct unknown pages to 404 ?

Posted: Tue Jul 16, 2013 2:45 pm
by Christopher
simonmlewis wrote:So do you "fake" the combined PHP page to make it appear as a 404 ?
You don't fake anything. The browser sends a request URL to the server. Your Front Controller determines if it can load code to generate a response to that request URL -- or not. If it cannot, then respond with a HTML page and a 404 HTTP header.

Re: How do you direct unknown pages to 404 ?

Posted: Wed Jul 17, 2013 3:16 am
by simonmlewis
I'm still lost as to what the final code should be.

Re: How do you direct unknown pages to 404 ?

Posted: Wed Jul 17, 2013 5:37 pm
by Christopher

Code: Select all

function getPage()
{
  $path = "includes/" . preg_replace('/[^A-Za-z0-9\.\-\_]/', '', $_GET['page']) . ".inc";

  if (! file_exists($path)) 
    header("HTTP/1.1 404 Not Found");
    $path= 'includes/error.inc';
  }
   include $path;
} 
You should be able to figure this out from the information given above.