Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to dynamically determine which htpasswd file to use at runtime? For example, the following (which does not work, just an example) inside a VirtualHost context:

RewriteEngine on
RewriteMap htpasswdfiles txt:/srv/htpasswdfiles/
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule .* - [E=PASSWD_FILE:${htpasswdfiles:%1},L]

<Directory /some/path>
    AuthType Basic
    AuthName "Restricted Access"
    AuthUserFile $PASSWD_FILE
    Require valid-user

I know the above example doesn't work, but i'm not sure if there's a different way to use environment variables like this... If not, i don't know if there would be any other method to choose the htpasswd file dynamically at runtime. would be a map containing domains on the left and the appropriate htpasswd file paths on the right.

share|improve this question

I used to setup dynamic basic auth using mod_perl with no htpasswd files at all, there was a handler declared in Apache configs and Perl handled the rest. But this is probably not what you want (mod_perl is not commonly used these days).

share|improve this answer
A handler for what ? – adaptr Aug 24 '12 at 10:13
@adaptr A handler for authentication requests, more info here:… – Alex Aug 24 '12 at 14:52

Solution 1

Just point your Apache-configuration to a flat file and have a script outside Apache control the content.

Solution 2

Use LDAP for authentication instead of looking up entries in a flat file.

share|improve this answer
If somebody can tell appart from the solutions (part 2 of the question) if i can or cannot use environment variables into httpd.conf (part 1 of the question, some references if possible) i'll mark that one as the correct answer... Thanks you all! – roirodriguez Aug 24 '12 at 10:01

Since rewriting and authentication have absolutely nothing in common, this does not work.

WHY do you need to "choose the htpasswd file dynamically"?

share|improve this answer
Because i'm lazy and i don't want to add a VirtualHost each time a user/domain gets added to the server :) I've found mod_perl so i know i can dynamically generate my configuration, which would solve my issue, but i didn't found a real answer to what i'm asking in apache docs, so i'm just asking... – roirodriguez Aug 24 '12 at 9:19
I know this doesn't work, just asking if any other method and if not why. Thanks for your answer! – roirodriguez Aug 24 '12 at 9:21
mod_perl offers no support whatsoever for "dynamically generating your configuration"; there is such a thing, called mod_macro, but it is not officially supported. – adaptr Aug 24 '12 at 10:14, for example... Haven't tried, but found a lot of links with that usage. – roirodriguez Aug 24 '12 at 10:16
..yeah, that is by definition not dynamic, as it requires a reload to take effect. – adaptr Aug 24 '12 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.