php - 通过 url 保护对文件和文件夹的访问

标签 php security .htaccess

在我的 .htaccess 文件中,我使用以下内容来防止直接访问文件夹:

Options -Indexes

我正在使用以下方法来阻止访问我们的关键文件:

<Files admin.php>
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from (my ipaddress)
</Files>

所以,现在用户无法访问 www.domain.com/scripts,因为它会抛出 404 错误,也无法访问 admin.php

但是如何防止直接访问所有内容?

例如,如果有人知道文件名,他们仍然可以访问它,例如:www.domain.com/scripts/process.php

要做什么?

最佳答案

使用 mod_rewrite:

将此规则置于所有其他规则之上:

RewriteRule ^scripts(/.*|)$ - [F,NC]

不使用 mod_rewrite:

将此代码放入 scripts/.htaccess 中:

Order Deny,Allow
Deny from all

更新:要阻止对所有文件的直接访问:

# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \..+[\s?]
# return forbidden error if not static files
RewriteRule (?!^.+\.(?:jpe?g|gif|bmp|png|tiff|css|js)$)^.*$ - [F]

关于php - 通过 url 保护对文件和文件夹的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771030/

相关文章:

java - 如何在ZAP中配置主动扫描输入 vector ?

php - .htaccess 使用 .htpasswd 和 wordpress 请求正确登录两次

.htaccess 文件导致 AJAX 出现 404 错误

django - 使用 Django,设置管理内容和静态内容的路径的正确方法是什么?

php - 如何在不循环的情况下检查数组是否具有 === null 的值?

php - 显示来自特定 WordPress 类别的精选帖子

php - 将 php 脚本放入图像标签的 src 中是否比正常情况慢,慢了多少?

python - 正则表达式的安全性

PHP - MySQL 安全问题?

php - 使用 PHP 打开和创建受密码保护的 zip 文件