I assume you don't want the URL to change because you're simulating an error page.
What you're describing is an internal redirect, which (as far as I can tell) isn't an option within PHP. What you can do is write no output from the script and
include() (or
virtual()) the error document. The standard approach would be to test at the top of the script anything that would fatally prevent the script from running. That way you don't need to worry about wasted processing or accidentally writing out data. Just to be safe, you still might want to use
output buffering.
If you're including an HTTP error page, I recommend using a scripted page so it can set the appropriate Status header.
404.php:
PHP:
<?php
$statusCode=404;
$statusName='Not Found';
$title='Page does not exist...';
// simple URL correction: find the longest head section of the URL that is a valid page
if (function_exists('apache_lookup_uri')) {
$uri = $_SERVER['REQUEST_URI'];
do {
$uri = dirname($uri);
$uri_info = apache_lookup_uri($uri);
} while ($uri_info->status >= 400 && $uri && $uri != '/');
}
$errorMessage=<<<EOF
<p>The page you requested ({$_SERVER['REQUEST_URI']}) does not exist, you can try visiting this site's home page below.<br />
<span style="font-size:26px;"><a href="http://{$_SERVER['HTTP_HOST']}/" class="Link">http://{$_SERVER['HTTP_HOST']}/</a></span>.
</p>
EOF;
include('err.php');
?>
err.php:
PHP:
<?php
if (! isset($errorCode)) {
$errorCode = $_SERVER{'REDIRECT_STATUS'};
}
header("Status: $statusCode $statusName");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...