Search code examples
apache.htaccessauthentication

Exclude one folder in htaccess protected directory


I have a directory protected by htaccess. Here is the code I use now:

AuthName "Test Area"
Require valid-user
AuthUserFile "/***/.htpasswd"
AuthType basic

This is working fine. However, I now have a directory inside of this folder that I would like to allow anyone to access, but am not sure how to do it.

I know that it is possible to just move the files outside of the protected directory, but to make a long story short the folder needs to stay inside the protected folder, but be accessible to all.

How can I restrict access to the folder, but allow access to the subfolder?


Solution

  • According to this article you can accomplish this by using SetEnvIf. You match each of the folders and files you want to grand access to and define an environment variable 'allow' for them. Then you add a condition that allows access if this environment variable is present.

    You need to add the following directives to your .htaccess.

    SetEnvIf Request_URI "(path/to/directory/)$" allow
    SetEnvIf Request_URI "(path/to/file\.php)$"  allow
    Order allow,deny
    Allow from env=allow
    Satisfy any