如何让Apache返回404,如果没有登录


How to have Apache return 404 if not logged in?

在我的网站https://internal.example.com上,/index.php文件是一个登录表单。我想配置Apache或PHP返回404错误的任何文件,除了/index.php,如果用户没有登录(而不是403,所以它是不明显的是什么文件在服务器上)。我该怎么做呢?

返回404标头很简单:

 <?php
 if($loggedIn == false){
   header("HTTP/1.0 404 Not Found");
 } else{
   //some other code
 }
 ?>

问题是只是改变标题为404不会显示404错误页,但只有一个空白页与标题设置为404(搜索机器人不会索引页面,但访问该网站的人将不知道发生了什么)。你必须自己添加404内容。

的例子:

<?php 
if($loggedIn == False){
header("HTTP/1.0 404 Not Found");
?>
<html> 
  <head>
    <title>404 Not Found</title>
  </head>
  <body>
    <h1>Not Found</h1>
    <p>The requested URL <?=$_SERVER['PHP_SELF']?> was not found on this server.</p>
    <hr>
    <address>Apache/x.x.xx (Debian) Server at xxx.xxx.xxx.xxx Port x</address>
  </body>
</html>
<?php 
exit;
} else {
  //some other code here
}
?>

    if (!$logged_in)){
      header('HTTP/1.0 404 Not Found');
    }
http://php.net/manual/en/function.header.php