有没有办法强制Apache返回404而不是403?

有没有办法如何configurationApache Web服务器返回404(未find)错误代码而不是403(禁止)的某些特定的目录,我想不允许访问?

我发现一些解决schemebuild议使用mod_rewrite,例如

RewriteEngine On RewriteRule ^.*$ /404 [L] 

由于发送404而不是403的目的是混淆目录结构,所以这个解决scheme太透露了,因为它redirect到了一些不同的位置,这使得最初访问的目录确实存在。

像例如RedirectMatch

 RedirectMatch 404 ".*\/\..*" 

做的伎俩,它禁止访问所有的文件或目录开始点,给出“404未找到”的错误。

从Apache手册:“重定向[匹配]指令通过要求客户重新获取新位置的资源将旧的URL映射到一个新的。” 默认情况下, 重定向发送302返回代码,但也可以返回其他状态代码,如上所示。

在遇到同样的问题之后,我用了下面的.htaccess文件

 Options -Indexes RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com [NC] RewriteRule ^(.*)/$ - [R=404,NC] 

第一行和第三行确保您不能列出文件夹内容,如果这样做,您将收到404错误。 RewriteCond指令确保这个重写规则只适用于主域。 由于我有几个子域,没有rewritecond,访问www.mydomain.com/subdomain也返回一个404,这不是我的意图。

你可以做这样的事情:

的.htaccess

ErrorDocument 403 /error/404.php

404.php

 <?php $status = $_SERVER['REDIRECT_STATUS'] = 404; header( $_SERVER['SERVER_PROTOCOL'] . ' ' . $status); ?> 404 Error 

要将所有403,400个错误更改为404错误,请将其放在/etc/apache2/conf.d/localized-error-pages或者.htaccess

 # Will raise a 404 error, because the file <fake_file_for_apache_404.php> doesn't exist. # We change 403 or 400 to 404 ! ErrorDocument 400 /fake_file_for_apache_404.php ErrorDocument 403 /fake_file_for_apache_404.php # We need to rewrite 404 error, else we will have "fake_file_for_apache_404.php not found" ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>" ErrorDocument 500 "server in update. Please comme back later."