How can I deny access to a complete folder and sub-folders, with the exception of one file? That file is: toon.php and resides in the same folder.

shareimprove this question
 
Order Allow,Deny
<FilesMatch "^toon\.php$">
Allow from all
</FilesMatch>

That is probably the most efficient that you can get.

shareimprove this answer
 
AuthGroupFile /dev/null
AuthName "Private Area"
AuthType Basic
AuthUserFile .htpasswd
require valid-user


<Files "toon.php">
Allow from all
Satisfy Any

</Files>

Works for me. Dont' forget to configure the .htpasswd file.

shareimprove this answer
 
   
I like this answer. Just want to add that you probably need AllowOverride AuthConfig Limit in your server config – Daniel Alder Sep 6 '14 at 10:05
Deny From All
<FilesMatch "^toon\.php$">
Allow From All
</FilesMatch>

Worked for me...

shareimprove this answer
 
2 
Is there some difference to this answer here? – AD7six Jul 24 '13 at 16:34
1 
yes, my code is more understandable =) – Georgy Spassky Oct 18 '13 at 8:31
2 
In what way =) -1 for duplicating an existing answer providing no further information. – AD7six Oct 18 '13 at 8:36

You may want to use the <Directory> and <Files> directives. You can look at the documentation here:

http://httpd.apache.org/docs/1.3/mod/core.html#directory

http://httpd.apache.org/docs/1.3/mod/core.html#files

In short, you want something like this:

<Directory /folder/without/access >
     Order Deny,Allow
     Deny from All
</Directory>

<Files "filename">
     Order Deny,Allow
     Allow from All
</Files>
shareimprove this answer
 

Add the following rule to htaccess file in root

RewriteEngine on


RewriteRule !toon\.php$ - [F]

This will deny access to all files and folders except toon.php .

shareimprove this answer
 

If, like me, you're looking for a way to have authentication on an entire site/folder except for a single file, you will find that this method is working very well:

#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user

#Allow valid-user
Deny from all
Allow from env=test_uri
Satisfy any

Example taken from : this site

shareimprove this answer
 

来自 https://stackoverflow.com/questions/3407543/htaccess-deny-access-to-all-except-to-one-file