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

时间:2009-09-28 10:13:11

标签: security apache mod-rewrite http-status-code-404 http-status-code-403

有哪些方法可以配置Apache Web服务器为某些特定目录返回404(未找到)错误代码而不是403(禁止),我想禁止访问它?

我找到了一些建议使用mod_rewrite的解决方案,例如。

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

由于发送404而不是403的目的是混淆目录结构,这个解决方案太具启发性了,因为它重定向到某个不同的位置,这显然最初访问的目录确实存在。

5 个答案:

答案 0 :(得分:27)

RedirectMatch,例如

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

诀窍,它禁止访问以点开头的所有文件或目录,给出“404 Not Found”错误。

从Apache手册:“Redirect [Match]指令通过要求客户端在新位置重新获取资源,将旧URL映射为新URL。”默认情况下,Redirect会发送302返回代码,但它也可以返回其他状态代码,如上所示。

答案 1 :(得分:7)

你可以做这样的事情:

<强>的.htaccess

  

ErrorDocument 403 /error/404.php

<强> 404.php

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

404 Error

答案 2 :(得分:6)

遇到同样的问题之后,我最终得到了以下.htaccess文件

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

第1行和第3行确保您无法列出文件夹内容,如果这样做,您将收到404错误。 RewriteCond指令确保此重写规则仅适用于主域。由于我有几个子域,没有重写,访问www.mydomain.com/subdomain也返回404,这不是我想要的。

答案 3 :(得分:1)

我认为在.htaccess中完成此任务非常丑陋。

有可能使其成为apache配置。看看:

添加到您的apache配置中:

ErrorDocument 403 /404

然后重新启动apache:

service apache2 restart

仅此而已。

答案 4 :(得分:-2)

要将所有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."