tags:

views:

596

answers:

2

Does anyone know if it's possible to use regex capture within Apache's DirectoryMatch directive? I'd like to do something like the following:

<DirectoryMatch ^/home/www/(.*)>
    AuthType Basic
    AuthName $1
    AuthUserFile /etc/apache2/svn.passwd
    Require group $1 admin
</DirectoryMatch>

but so far I've had no success.

Specifically, I'm trying to create a group-based HTTP Auth for individual directories/vhosts on a server in Apache 2.0.

For example, Site A, pointing to /home/www/a will be available to all users in group admin and group a, site b at /home/www/b will be available to all users in group admin and group b, etc. I'd like to keep everything based on the directory name so I can easily script adding htpasswd users to the correct groups and automate this as much as possible, but other suggestions for solving the problem are certainly welcome.

A: 

What you are trying to do looks very similar to per-user home directories (see http://httpd.apache.org/docs/2.0/howto/public_html.html). The way Apache handles these is through file system permissions and .htaccess files (see http://httpd.apache.org/docs/1.3/howto/htaccess.html). I don't believe there is any way to use regex capture in the enclosed directives (AuthName, etc).

+1  A: 

You could tackle the problem from a completely different angle: enable the perl module and you can include a little perl script in your httpd.conf. You could then do something like this:

<Perl>
my @groups = qw/ foo bar baz /;
foreach ( @groups ) {
    push @PerlConfig, qq| <Directory /home/www/$_> blah </Directory> |;
}
</Perl>

That way, you could even read your groups and other information from a database or by simply globbing /home/www or whatever else tickles your fancy.

innaM