Search code examples
.htaccess.htpasswd

.htaccess: Different AuthUserFile paths for different hosts


I am developing an application that has to run both locally as well as on a remote server. One directory is protected using an htpasswd file. This how I have to define the path to the htpasswd file in the htaccess file:

AuthUserFile ../app/some-folder/.htpasswd

Unfortunately the app folder is required on the server, but I don't have that locally. Instead, the path has to look like this:

AuthUserFile /Volumes/some/very/long/path/some-folder/.htpasswd

Is it possible to us en if/else statement to switch this AuthUserFile path according to the current host (Edit: Or better yet, don't require any authentication locally)? Or is there another workaround to make this file work on both my local and the remote host?


Solution

  • Never mind, I figured it out. I put this in my .htaccess:

    AuthName "Protected"
    AuthType Basic
    AuthUserFile path/to/.htpasswd
    Require valid-user
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Satisfy Any
    

    This allows any connections made from the local IP to go through without entering a password. Not perfect, but works in my situation.