【问题标题】:.htaccess is still allowing access to files.htaccess 仍然允许访问文件
【发布时间】:2020-07-02 18:25:22
【问题描述】:

如果我想阻止随机用户使用 URL 浏览网页文件,我需要使用 .htaccess 文件。

我在下面添加了我的代码。我创建了一个 .htaccess 文件并将其放在我的包含文件夹中,以防止用户导航到和读取我的 database.php 文件。

按照此处的说明进行操作:https://www.plothost.com/kb/how-to-deny-access-to-a-specific-file-on-your-site-via-htaccess/

当然我做了一些细微的改动。

这是我的 .htaccess 文件中的代码:

<files database.php>
Order Allow,Deny
Deny from all
</files>

使用上述方法,我仍然可以通过 URL 访问 database.php 文件。我需要防止这种情况发生。

我做错了什么?

【问题讨论】:

  • @aviboy2006 - 我尝试了该页面上的前两个答案。我仍然可以使用第一个答案直接访问该文件夹。第二个,虽然看起来确实有效,但是回到index.php时,数据库好像断开了。
  • @JohnBeasley 如果我的解决方案对您有用,请告诉我。

标签: php .htaccess server


【解决方案1】:

请检查以下内容:

  1. 确保您的 .htaccess 文件确实被称为“.htaccess”
  2. .htaccess 文件必须位于正确的目录中或文件的路径必须相对于 .htaccess 文件。

我刚刚在我的机器上运行了这个。您使用的相同代码。 我的结构:

文件完全相同。我注释掉了根目录中 .htaccess 文件中的内容。所以现在我可以打电话给localhost:8080/database.php,但不能打电话给localhost:8080/test/database.php => 我得到一个Error 403(拒绝访问)。

编辑

这里的指南怎么样? setup htaccess

看起来合法。这是我的配置。根据指南,只需设置此配置文件并重新启动即可。

编辑 2

我发现,我打开的 httpd.conf 不是正确的。 我在下面找到了正确的:Application/XAMPP/xamppfiles/etc/httpd.conf.

在这个文件中你必须搜索:

# 
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/trunk/mod/core.html#options
    # for more information.
    #
    #Options Indexes FollowSymLinks
    # XAMPP
    Options Indexes FollowSymLinks ExecCGI Includes

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    # since XAMPP 1.4:
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

在这里你可以找到这个:

#AllowOverride None  # this does deactivate .htaccess
# since XAMPP 1.4:
AllowOverride All    # this does activate .htaccess

至少对我来说,这完全是 .htaccess 重写的原因。当我将其设置为 AllowOverride None 时,.htaccess 将被完全忽略。

【讨论】:

  • 我的包含文件夹中有我的 .htaccess 文件。我的包含文件夹中仅有的 2 个文件是 database.php 和 .htaccess(使用上面列出的代码),但我仍然可以通过 URL 访问该文件。想法?
  • @JohnBeasley 尝试将 .htaccess 移动到您的根目录我将检查包含文件夹的部分...等等。
  • 我把文件移到了根目录,但还是不行。
  • 尝试将Order Allow,Deny(换行符)Deny from all 放入.htaccess 中(只需删除文件标签),看看会发生什么。
  • @JohnBeasley 哈哈。不用担心。好吧,至少您已经发现 .htaccess 必须在 apache 配置中启用。这就是您的解决方案不起作用的原因。我假设您使用 xampp oder 灯之类的东西?我只是在 Windows 和 iOS 上做到了这一点,我从来不需要真正配置任何东西。顺便说一句,你的设置是什么?
【解决方案2】:

您可以在站点根目录.htaccess 中使用mod_rewrite 规则来执行此操作:

RewriteEngine On

RewriteCond %{THE_REQUEST} /database\.php[?\s/] [NC]
RewriteRule ^ - [F]

【讨论】:

  • 你应该注释掉你完整的 &lt;files 块(所有 4 行)并确保除了 root 之外没有其他 .htaccess
  • 还是没有运气。我仍然可以直接通过 URL 访问该文件。想法?
  • 有趣...我没有收到任何 500 错误。我猜这意味着 .htaccess 没有启用?
  • 它因操作系统和 Apache 安装而异。请在谷歌上搜索
  • 感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
相关资源
最近更新 更多