.htaccess甚至阻止php访问


.htaccess blocking even php access

我想拒绝访问包含数据库详细信息的txt文件,但使用当前方法,我发现即使是脚本也无法访问所需的详细信息。我将定期将这个设置从windows移到linux,然后再移回来,所以需要一个不脆弱的解决方案。

# Refuse direct access to all files
Order allow,deny
Allow from 127.0.0.1
Deny from all

不是最强的安全方法,但您可以在DOCUMENT_ROOT/.htaccess文件中使用这样的规则:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www'.)?yourdomain'.com/ [NC] 
RewriteRule ^secret'.txt$ - [F,NC]

secret.txt替换为您的txt文件名,将yourdomain.com替换为您实际的域名

这将阻止从任何地方访问secret.txt,除非REFERRER来自您自己的网站。