Search code examples
apache.htaccess.htpasswd

htaccess restrict authentification but exception on file


I've seen a few other relative posts but didn't find any solution.

I need to restrict a folder with authentification, it works. But in this folder I need to keep one file access opened to everybody, I used this but it doesn't works :

AuthName "Admins Only"
AuthUserFile /home/dd/.htpasswd
AuthGroupFile /dev/null
AuthType basic
require user AuthorizedUser
Options -Indexes
<Files "admin-ajax.php">
   Allow from all
   Satisfy all
</Files>

Sorry for my bad english and thanks for help !


Solution

  • use SetEnv and Order directive :

    #set variable if uri is "/admin-ajax.php"
    SetEnvIf Request_URI ^/folder/admin-ajax\.php noauth=1
    
    #auth 
    AuthName "Admins Only"
    AuthUserFile /home/dd/.htpasswd
    AuthGroupFile /dev/null
    AuthType basic
    require user AuthorizedUser
    
     #Here is where we allow/deny
    Order Deny,Allow
    Satisfy any
    Deny from all
    Require user AuthorizedUser
    Allow from env=noauth
    

    This will let you access your admin-ajax.php without login to server.