I'm looking for a way to force Apache to look for files relatively to DocumentRoot not to ServerRoot. Now if I create .htaccess as follows:
#Protect Directory
AuthName "Please provide valid username and password"
AuthType Basic
AuthUserFile .htpasswd
Require valid-user
Apache looks for the .htpasswd in:
/opt/homebrew/opt/httpd/.htpasswd
and I would like to look for it in:
/opt/homebrew/var/www/FOLDER/.htpasswd
.htaccess is placed in:
/opt/homebrew/var/www/FOLDER/.htaccess
I don't want to change ServerRoot and move all the configuration.
You cannot change this behaviour. As per the Apache docs for the
AuthUserFiledirective, the file-path given to theAuthUserFiledirective must be either:An absolute filesystem path. For example:
OR,
ServerRoot.However, assigning a path relative to the document root would also go against security recommendations, that specifically states:
In your example (
AuthUserFile .htpasswd) it looks like you are wanting this to be relative to the directory that contains the.htaccessfile, not just the document root?If you need to serve different configs on different servers then you can use
<If>expressions (or other conditionals) to setAuthUserFileconditionally based on elements of the request or server.You could also use defined variables. For example:
In
.htaccess: