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/htpasswdfiles.map
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. htpasswdfiles.map would be a map containing domains on the left and the appropriate htpasswd file paths on the right.

3 Answers 3


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).


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.

  • 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! Aug 24, 2012 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"?

  • 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... Aug 24, 2012 at 9:19
  • I know this doesn't work, just asking if any other method and if not why. Thanks for your answer! Aug 24, 2012 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, 2012 at 10:14
  • ubuntuforums.org/archive/index.php/t-1662752.html, for example... Haven't tried, but found a lot of links with that usage. Aug 24, 2012 at 10:16
  • ..yeah, that is by definition not dynamic, as it requires a reload to take effect.
    – adaptr
    Aug 24, 2012 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .