不允许通过.htaccess访问root中的txt文件

时间:2014-05-19 15:10:35

标签: php .htaccess

我有自定义MVC框架,我的.htaccess文件重写规则,通过index.php发送所有请求,然后路由请求。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|jpeg|JPG|gif|png|PNG|ico|pdf|swf|xml|pem)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

问题是我在public_html文件夹中有一些 .txt 文件,其中包含index.php和.htaccess。是否有可能不允许用户访问.txt文件?现在如果我访问www.MyURL/test.txt我可以查看txt文件。但如果我www.MyURL/cont/action/param1可以正常工作。

1 个答案:

答案 0 :(得分:4)

您可以使用以下方法阻止所有.txt个文件:

RewriteEngine on

RewriteRule \.txt$ - [F,NC]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|jpeg|JPG|gif|png|PNG|ico|pdf|swf|xml|pem)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]