Password Protect All but One File (htaccess)

Once in a while need to allow someone access to one file but no other files in the same directory. I often solve this problem by moving the one file to a sub directory and then adding the following to an htaccess file in that same sub directory.

allow from all
satisfy any

This normally works well but is not a perfect solution since it is not always appropriate to move the file to a different directory. So, I took the time and figured out how to password protect everything but one file. Below is the basic password protection that I had in the htaccess file that blocked everything in the directory (and sub-directories).

AuthGroupFile /dev/null
AuthName "A Blog"
AuthType Basic
AuthUserFile /path/to/.htpasswd
require valid-user

Now in order to exclude a file I just had to add the following below the above six lines of code.


  Allow from all
  Satisfy any

Of course this can be taken one step further if you wanted to exclude multiple files from password protection.


  Allow from all
  Satisfy any

FilesMatch can take a regular expression so you don't necessarily have to list out each file. The below code will also accomplish the same thing (as above).


  Allow from all
  Satisfy any

Related Posts:

2 Responses to “Password Protect All but One File (htaccess)”

  1. RL Says:

    Thank you sir, you're a lifesaver :)

  2. Greg Says:

    Good article! Completely solved my problem, I was busy playing with in the httpd.conf…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.