How do you direct unknown pages to 404 ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you direct unknown pages to 404 ?

Post 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 } " ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you direct unknown pages to 404 ?

Post 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.
clark551
Forum Commoner
Posts: 25
Joined: Mon Jul 01, 2013 1:49 pm

Re: How do you direct unknown pages to 404 ?

Post by clark551 »

header("Location: err.php?e=404");
you can also use this to direct your unknown pages to 404...
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you direct unknown pages to 404 ?

Post 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 ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you direct unknown pages to 404 ?

Post 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.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you direct unknown pages to 404 ?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you direct unknown pages to 404 ?

Post 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.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you direct unknown pages to 404 ?

Post by simonmlewis »

I'm still lost as to what the final code should be.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do you direct unknown pages to 404 ?

Post 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.
(#10850)
Post Reply