如何拒绝访问.htaccess中的文件

时间:2012-07-30 20:21:29

标签: .htaccess

我有以下.htaccess文件:

RewriteEngine On
RewriteBase /

# Protect the htaccess file
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>

# Protect log.txt
<Files ./inscription/log.txt>
Order Allow,Deny
Deny from all
</Files>

# Disable directory browsing
Options All -Indexes

我试图禁止访问者访问以下文件:

domain.com/inscription/log.txt

但我上面的内容不起作用:我仍然可以远程从浏览器访问该文件。

5 个答案:

答案 0 :(得分:162)

在htaccess文件中,<Files>指令的范围仅适用于该目录(我猜想当子目录的htaccess中的规则/指令应用于父目录中的超级指令时,会避免混淆。)

所以你可以:

<Files "log.txt">  
  Order Allow,Deny
  Deny from all
</Files>

对于Apache 2.4+,您可以使用:

<Files "log.txt">  
  Require all denied
</Files>

inscription目录中的htaccess文件中。或者您可以使用mod_rewrite来处理这两种情况,拒绝访问htaccess文件以及log.txt:

RewriteRule /?\.htaccess$ - [F,L]

RewriteRule ^/?inscription/log\.txt$ - [F,L]

答案 1 :(得分:15)

强模式匹配 - 这是我在Perishable Press中使用的方法。使用强模式匹配,此技术可防止外部访问包含“ .hta ”,“ .HTA ”或任何不区分大小写的组合的任何文件。为了说明,此代码将阻止通过以下任何请求进行访问:

  • 的.htaccess
  • 的.htaccess
  • 的.htaccess
  • testFILE.htaccess
  • filename.HTACCESS
  • FILEROOT.hTaCcEsS

..等等。很明显,这种方法在保护您网站的HTAccess文件方面非常有效。此外,该技术还包括强化“满足所有”指令。请注意,此代码应放在域的根HTAccess文件中:

# STRONG HTACCESS PROTECTION
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>

答案 2 :(得分:11)

我不相信目前接受的答案是正确的。例如,我在虚拟服务器的根目录中有以下.htaccess文件(apache 2.4):

<Files "reminder.php">
require all denied
require host localhost
require ip 127.0.0.1
require ip xxx.yyy.zzz.aaa
</Files>

这可以防止对子目录中reminder.php的外部访问。 我的Apache 2.2服务器上有一个类似的.htaccess文件具有相同的效果:

<Files "reminder.php">
        Order Deny,Allow
        Deny from all
        Allow from localhost
        Allow from 127.0.0.1
     Allow from xxx.yyy.zzz.aaa
</Files>

我不确定,但我怀疑它是在.htaccess文件中明确定义子目录的尝试, viz <Files ./inscription/log.txt>导致它失败。将.htaccess文件放在与log.txt相同的目录中更简单,即在inscription目录中,它将在那里工作。

答案 3 :(得分:2)

将以下行放在.htaccess文件中,并根据需要替换文件名

RewriteRule ^(test\.php) - [F,L,NC]

答案 4 :(得分:-4)

您可以使用<Directory>标记 例如:

<Directory /inscription>
  <Files log.txt>
    Order allow,deny
    Deny from all
  </Files>
</Directory>

请勿使用./,因为如果您只使用/,则会查看您网站的根目录。

有关更详细的示例,请访问http://httpd.apache.org/docs/2.2/sections.html

相关问题