Apache authentication for multiple directories

apache-2.2

I've website running in apache 2.2.3. Root directory of website contains three directories. I want to setup authentication for these directories as below

  1. only user1 can browse all pages

  2. user2 can only access mydomain.com/dir1/

  3. user3 can only access mydomain.com/dir2/

  4. user4 can only access mydomain.com/dir3/

I've tried below configuration but only user1 can browse website other users are unable to access website.

<directory /var/www/html/example>
        AuthUserFile /etc/httpd/user1-htpasswd
        AuthName "Resricted"
        AuthType Basic
        Require user user1
</directory>

<directory /var/www/html/example/dir1/>
        AuthUserFile /etc/httpd/user2-htpasswd
        AuthName "Resricted"
        AuthType Basic
        Require user user2
</directory>

<directory /var/www/html/example/dir2/>
        AuthUserFile /etc/httpd/user3-htpasswd
        AuthName "Resricted"
        AuthType Basic
        Require user user3
</directory>

Please guide me for the same. Thanks in advance.

Best Answer

It should work if you put all your users login info in one passwd file and then do something like this (untested)

<directory /var/www/html/example/dir1/>
        AuthUserFile /etc/httpd/users-htpasswd
        AuthName "Resricted"
        AuthType Basic
        Require user user1
        Require user user2
</directory>

<directory /var/www/html/example/dir2/>
        AuthUserFile /etc/httpd/users-htpasswd
        AuthName "Resricted"
        AuthType Basic
        Require user user1
        Require user user3
</directory>
Related Topic