あなたの見かけのファイル構造:
/
.htaccess
request.php
...
errors/
junk.php
.htaccess
ErrorDocument 404 /errors/junk.php
request.php
header('HTTP/1.1 404 Not Found', true, 404);
echo "Despite the 404 header this ~file~ actually exists as far as Apache is concerned.";
exit;
errors / junk.php
header('HTTP/1.1 404 Not Found', true, 404);
echo "The file you're looking for ~does not~ exist.";
echo "<pre>" . var_export($_SERVER, TRUE) . "</pre>";
exit;
http://yoursite.com/request.phpには次のように表示されます。
404ヘッダーにもかかわらず、この〜file〜は、Apacheに関する限り実際に存在します。
http://yoursite.com/filethatdoesntexist.phpには次のように表示されます。
探しているファイルは〜存在しません〜。
[カスタム404ハンドラーコードの記述に役立つ可能性のある$_SERVERのダンプ]
存在するファイルがあるが、それが404であるかのように見せたい場合は、次のようにPHPでリダイレクトを記述できます。
header('Location: http://mysite.com/errors/junk.php');
exit;
ブラウザを完全なURLにリダイレクトするか、単に次のようにします。
include('errors/junk.php');
exit;
これにより、ユーザーは同じページURLに残りますが、エラーコードが表示されます。