欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to deny access to a file in .htaccess


I have the following .htaccess file:

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

I am trying to forbid visitors to access the following file:

domain.com/inscription/log.txt

but what I have above does not work: I can still access the file from the browser remotely.

shareimprove this question
 
1 
Why are you denying access to the .htaccess file? – tonybaldwin Apr 1 '15 at 20:39

5 Answers 正确答案

up vote115down voteaccepted

Within an htaccess file, the scope of the <Files> directive only applies to that directory (I guess to avoid confusion when rules/directives in the htaccess of subdirectories get applied superceding ones from the parent).

So you can have:

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

In an htaccess file in your inscription directory. Or you can use mod_rewrite to sort of handle both cases deny access to htaccess file as well as log.txt:

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

RewriteRule ^/?inscription/log\.txt$ - [F,L]
shareimprove this answer
 
   
If your using IIS, do this in the web.config file instead. – Shaun Luttin Nov 3 '14 at 16:33
   
Using the rewrite rules denies my own code from accessing the contents of *.txt. How would you get around this? – Pantss Dec 6 '15 at 5:34
2 
"the scope of the <Files> directive only applies to that directory" - no it doesn't. It applies to that directory and all subdirectories below it. So <Files "log.txt"> will catch /log.txt and/inscription/log.txt. – MrWhite Jan 30 at 15:45

Strong pattern matching — This is the method that I use here at Perishable Press. Using strong pattern matching, this technique prevents external access to any file containing “.hta”, “.HTA”, or any case-insensitive combination thereof. To illustrate, this code will prevent access through any of the following requests:

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

..etc., etc. Clearly, this method is highly effective at securing your site’s HTAccess files. Further, this technique also includes the fortifying “Satisfy All” directive. Note that this code should be placed in your domain’s root HTAccess file:

# STRONG HTACCESS PROTECTION
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
shareimprove this answer
 
   
I get a 500 internal server error when using this code. Any idea why? I'm using Wordpress. – Keryn Gill Jan 26 '15 at 21:25
3 
Wouldn't it make more sense to just disallow users from accessing dotfiles? Using something like <Files ~"^\..*">. – Time Sheep Nov 15 '16 at 9:12

I don't believe the currently accepted answer is correct. For example, I have the following .htaccess file in the root of a virtual server (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>

This prevents external access to reminder.php which is in a subdirectory. I have a similar .htaccess file on my Apache 2.2 server with the same effect:

<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>

I don't know for sure but I suspect it's the attempt to define the subdirectory specifically in the .htaccess file, viz <Files ./inscription/log.txt> which is causing it to fail. It would be simpler to put the .htaccess file in the same directory as log.txt i.e. in the inscription directory and it will work there.

shareimprove this answer
 

Place below line in your .htaccess and replace file name as your wish

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

Well you could use the <Directory> tag for example:

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

Do not use ./ because if you just use / it looks at the root directory of your site.

For a more detailed example visit http://httpd.apache.org/docs/2.2/sections.html

shareimprove this answer
 
   
But a <Directory> container is not permitted in .htaccess. In .htaccess, the .htaccess file itself is the "Directory container" (per-directory config file). – MrWhite Jan 30 at 13:47
   
Well then you could use the Redirect. Redirect /inscription/log.txt access_denied.html This will redirect the user to access_denied.html if the user goes to inscription/log.txt in the inscription directory. – Nicholas English Jan 30 at 14:47

来自 https://stackoverflow.com/questions/11728976/how-to-deny-access-to-a-file-in-htaccess


普通分类: