【问题标题】:Problem redirecting 403 Forbidden to 404 Not Found将 403 Forbidden 重定向到 404 Not Found 时出现问题
【发布时间】:2010-10-07 14:00:03
【问题描述】:

我的 .htaccess 的相关部分如下所示:

Options -Indexes
<FilesMatch include>
    Order allow,deny
    Deny from all
</FilesMatch>
RedirectMatch 404 ^/include(/.*)$

它正在生成以下响应:

  • /包括403
  • /包括/ 404
  • /include/config.inc 403

通过查看我的模式,我可以看出问题可能出在 (/.*) 部分,但我尝试过的所有操作都给了我相同的结果;而不是一直得到 404,我得到一个案例的 404 和其他所有案例的 403。 我使用的表达式有什么问题?或者,由于我必须为几个目录执行此操作,是否有一种全面的方法可以让我将所有 403 响应转换为 404?

更新:我发现通过删除 FileMatch 可以获得更好的结果,所以我的 .htaccess 现在看起来像这样:

Options -Indexes
RedirectMatch 404 ^/include(/.*)?$ # Added dlamblin's first suggestion

并生成以下响应:

  • /包括404
  • /包括/ 404
  • /include/config.inc 403

更新:有趣的是,我发现以下会产生不同的输出:

RedirectMatch 404 ^/include(/?|/.*)$
RedirectMatch 404 ^/template(/?|/.*)$

模板模式适用于所有情况,但是 include 仍然为 include 中的所有文件生成 403(例如 /include/config.inc)这可能是目录名称的问题,而不是 .htaccess 的问题文件本身?

更新:在访问 /include/config.inc 时,我的 .htaccess 中的以下内容与重定向冲突。

<FilesMatch config>
    Order allow,deny
    Deny from all
</FilesMatch>

【问题讨论】:

  • 这是很久以前的事了,但我想建议您只需将包含和模板放在文档根目录之外的某个位置即可解决所有这些问题;这是最难搞砸的方法,而且它在 Apache 上的启动负载略少!
  • 最快最简单的解决方案,只需要ErrorDocument指令和一个PHP页面就可以解决404错误:Just use http_response_code.不用乱搞RedirectMatchRewriteEngine

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


【解决方案1】:

我可以理解为什么 /include 没有被您的 RedirectMatch 捕获,您没有将结尾 '/' 设为可选,但是 /include/config.inc 部分有点令人费解。

这是我在 Apache 2.2 上的工作:

<FilesMatch /include(/?|/.*)>
    Order allow,deny
    Deny from all
</FilesMatch>

RedirectMatch 404 ^/include(/?|/.*)$

这可以处理这些情况:

/include 404
/include/ 404
/include/config.inc 404

我必须更改 FilesMatch 部分才能使 /include 部分正常工作。

编辑:

匹配行也可以在 .htaccess 中没有 部分的情况下工作,并给出预期的结果。

【讨论】:

  • 肯定有其他原因导致问题,因为使用此 /include/config.inc 仍会生成 403。我将在我的 httpd.conf 中进行一些挖掘,看看是否有什么问题干扰。
  • RewriteRule ^/include(/?|/.*)$ - [R=404,NC,L] 有什么问题?
  • @bryan-rehbein 它不适用于“include/.php”和“include/.htaccess”,它返回 403 而不是 404
【解决方案2】:

另一种可能性是不用费心匹配整个路径:

RedirectMatch 404 ^/include

如果有可能以“/include”开头的公开可见的 URL 路径(例如,“/includeMe”),则添加一个小部分会将私有 URL 与公共 URL 分开:

RedirectMatch 404 ^/include(/|$)

【讨论】:

    【解决方案3】:

    带重写模组:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} ^.*/\.
    RewriteRule ^(.*)$ - [R=404]
    

    每个以点开头的文件或目录都会被重定向到 404。

    /myDir/.svn => 404
    /.gitignore => 404
    /dir1/dir2_dir3/
    

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

    【讨论】:

      【解决方案4】:

      你不想要'^/include(/.*)吗?$'

      这部分是一个猜测,但是如果将 RedirectMatch 放在块上方会发生什么。这样,您就不会在将请求重定向到 404 之前拒绝(禁止)访问该请求。

      【讨论】:

        【解决方案5】:

        更好更简单的方法如下:

        1 - 插入您的主配置文件(即外部 VHOST 世界):

        <IfModule mod_alias.c>
        RedirectMatch 404 ^/myDirectory(?|\/)$
        </IfModule>
        

        与 myDirectory = css、js、图像 ...

        2 - 您可以按如下方式设置每个目录的索引:

        允许提供您的“DirectoryIndex”Apache 指令内容:

        <Directory "/myPathtoMyWebDocRoot/">
            Options Indexes
            AllowOverride None
            Order Allow, Deny
            Allow from all
        </Directory>
        

        并拒绝其他人的目录索引:

        <Directory "/myPathtoMyWebDocRoot/myDirectory/">
            Options -Indexes 
            AllowOverride None
            Order Allow, Deny
            Allow from all
        </Directory>
        

        with myDirectory = *, css, images, js, secret, ... 满足您的需求。

        【讨论】:

          【解决方案6】:

          对于所有名称以点开头的文件/目录,即使在子目录中也可以正常工作:

          <FilesMatch "^\..+">
              Order allow,deny
              Deny from all
              Satisfy All
          </FilesMatch>
          RedirectMatch 404 \/\..+$
          

          【讨论】:

            【解决方案7】:

            在这种情况下,您不需要使用Options -Indexes 我停止将目录内容显示为列表并显示 404 错误的解决方案很简单。在项目的根目录中创建 .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 时请记住清除浏览器上的缓存。

            【讨论】:

              【解决方案8】:
              ErrorDocument 400 /fake_file_for_apache_404.php
              ErrorDocument 403 /fake_file_for_apache_404.php
              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."
              

              我尝试了这个解决方案,但遇到了另一个问题:

              此外,在尝试使用 ErrorDocument 处理请求时遇到 403 Forbidden 错误:

              【讨论】:

                猜你喜欢
                • 2014-05-11
                • 1970-01-01
                • 2017-12-23
                • 1970-01-01
                • 1970-01-01
                • 2017-06-23
                • 1970-01-01
                • 1970-01-01
                • 2017-09-30
                相关资源
                最近更新 更多