代码之家  ›  专栏  ›  技术社区  ›  fuenfundachtzig

有没有办法强迫阿帕奇返回404而不是403?

  •  25
  • fuenfundachtzig  · 技术社区  · 16 年前

    有没有一种方法可以配置ApacheWeb服务器为我不允许访问的某些特定目录返回404(未找到)错误代码而不是403(禁止访问)?

    我发现了一些建议使用mod重写的解决方案,例如

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

    由于发送404而不是403的目的是混淆目录结构,因此该解决方案过于暴露,因为它重定向到了某个不同的位置,这使得最初访问的目录实际上确实存在。

    4 回复  |  直到 10 年前
        1
  •  26
  •   fuenfundachtzig    16 年前

    RedirectMatch 如在

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

    做的诀窍,它禁止访问所有的文件或目录以一个点开始,给出“404没有找到”的错误。

    来自Apache手册:“redirect[match]指令通过要求客户机在新位置重新获取资源,将旧的URL映射到新的URL。”默认情况下, Redirect 发送302返回代码,但也可以返回上面所示的其他状态代码。

        2
  •  6
  •   jff    12 年前

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

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

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

        3
  •  5
  •   Tomer    13 年前

    你可以这样做:

    HTAccess

    错误文档403/error/404.php

    404.php西班牙语

    <?php
    $status = $_SERVER['REDIRECT_STATUS'] = 404;
    header( $_SERVER['SERVER_PROTOCOL'] . ' ' . $status);
    ?>
    
    404 Error
    
        4
  •  -2
  •   ImmortalPC    12 年前

    要将所有403400个错误更改为404个错误,请在 /etc/apache2/conf.d/本地化错误页面 或进入 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."