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

问题描述:

I have custom MVC framework and my .htaccess file rewrites rules to send all requests via index.php which then routes the requests.

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]

Problem is that I have some .txt files in public_html folder where index.php and .htaccess reside. Is it possible to not allow users access .txt files? Right now If I visit www.MyURL/test.txt I can view the txt file. But if I did www.MyURL/cont/action/param1 would work correctly.

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

  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] 
  code>  pre> 
 
 

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

You can just block all .txt files using:

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]