0

I have the following code in my .htaccess file

AuthUserFile ./.htpasswd
AuthGroupFile /dev/null
AuthName "Dev Area One"
AuthType Basic

<Limit GET>
require valid-user
</Limit>

This causes a 500 internal server error because Apache says it can't find the file /etc/apache2/.htpasswd. That's because the .htpasswd is in the directory /var/www/html/dev1.staging.com/public_html/.htpasswd and my .htaccess is in /var/www/html/dev1.staging.com/public_html/.htaccess.

Can I replace AuthUserFile ./.htpasswd with AuthUserFile SOMEVARIABLE+.htpasswd so that every time other team members do a git pull origin master to their dev servers, the .htaccess will properly reference the .htpasswd which should be in the same directory as the .htaccess?

This is purely for development purposes only. There are some servers (some of which are shared hosting) where we do not have file writing access to, so we don't know immediately what directory our web projects are served out of, so we don't know ahead of time the absolute directory path to the .htpasswd.

John
  • 32,403
  • 80
  • 251
  • 422
  • use the absolute path to the .htpasswd file. Why can't you do that? – Panama Jack Oct 17 '16 at 14:14
  • @PanamaJack How do I determine the absolute path ? I do not have SFTP or shell on the clients server. And the client refuses to manually modify any files on their staging servers. – John Oct 17 '16 at 14:25
  • Dude you already have it. `AuthUserFile /var/www/html/dev1.staging.com/public_html/.htpasswd` Try it that way. I use the full path and it always works for me. – Panama Jack Oct 17 '16 at 14:47
  • @PanamaJack that's only my server. THat's not going ot be the path on our client servers. And our clients are not being cooperative in exposing their directory structure on the servers. They say "just merge your changes from git dev branch to the master branch, and our codeship will automatically deploy to our servers. Do not ask us any more questions about our servers, because you are not supposed to know about it.". – John Oct 17 '16 at 14:54
  • I see. I didn't understand it was another server. Then maybe they don't need the help then. :) Then have them make the change in the .htaccess file to their path. – Panama Jack Oct 17 '16 at 15:14

1 Answers1

0

You can use shell environment variable in path to AuthUserFile by using PassEnv directive:

PassEnv HOME

AuthUserFile ${HOME}/.htpasswd
AuthGroupFile /dev/null
AuthName "Dev Area One"
AuthType Basic

<Limit GET>
require valid-user
</Limit>

Above snippet will work only if each user stores .htpasswd directly under $HOME directory.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks this is useful to know for the future. Unfortunately for my current situation, the `.htpasswd` my not always be in the `$HOME` directory. It will vary from server to server – John Oct 17 '16 at 14:48
  • Then try using some other variable that you can control from one server to another. This is only possible way to use some variable in `AuthUserFile`, which uses hardcoded path otherwise. – anubhava Oct 17 '16 at 14:50