在我的 .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/