用404而不是403来禁用目录列表

我已经禁用目录列表像这样…

Options -Indexes 

当我尝试访问这样的目录:

 www.example.com/secret/ 

我得到了一个403禁止回应。

但是,我想要一个404 Not Found响应,所以黑客无法轻易猜测我的目录结构。 我该怎么做?

在/ secret中启用mod_rewrite和AllowOverride。 然后创build.htaccess:

 RewriteEngine On RewriteBase /secret RewriteRule ^$ - [R=404,L] 

我已经环顾了互联网寻找类似问题的答案。 虽然mod_rewrite是一个可能的解决scheme,我发现最好的解决scheme使用“RedirectMatch”指令。

请参阅StackOverflow:问题redirect403禁止404找不到

我的解决scheme,以停止显示目录的内容为列表,并显示404错误很简单。 在项目的根目录下创build.htaccess文件,并写出应该保护的目录。

目录结构

 - your_root_directory - .htaccess - 404.html - index.html - app - other files I do not want to show - models - other files I do not want to show 

的.htaccess

 RewriteEngine On RewriteRule ^app/ - [R=404,L] RewriteRule ^models/ - [R=404,L] ErrorDocument 404 /your_root_directory/404.html 

.htaccess的第二行禁止访问app目录及其所有子目录中的列表项。

.htaccess的第三行不允许访问模型目录及其所有子目录中的列表项。

第四个.htaccess行设置我们自己的404错误(如果你不想显示Apache的默认错误)。

请记住在使用htaccess时清除浏览器上的caching。

创build一个返回404错误的自定义403脚本。

例如,在PHP中:

 <?php header("HTTP/1.0 404 Not Found"); die("<h1>404 Not Found</h1>"); ?> 

现在将Apacheconfiguration为使用此脚本获得403个结果:

 ErrorDocument 403 /403.php 

没有搞乱重写,它立即为整个服务器工作。

使用.htaccess来掩盖错误。 请参阅本指南:

http://www.tarahost.net/pages/web-design/29371.php