1

On my development machine osx-sierra / apache 2.4.10 (from brew) I have a restriction in a VirtualHost that allow access to /api/ without password, and all others pages need the password with this code:

<Location />
    AuthType Basic
    AuthName "Access"
    AuthUserFile /Users/xxxxxx/www/public/.htpasswd
    Require expr %{REQUEST_URI} =~ m#^/api/*#
    Require valid-user
</Location>

When I try to do it on my production server with the same directives, Debian-jessie / apache 2.4.29 (from apt), it doesn't work, password is always ask (chrome/safari/wget), I tryed theses solutions :

1/

<Location />
    AuthType Basic
    AuthName "Access"
    AuthUserFile /home/xxxxxx/www/public/.htpasswd
    Require expr %{REQUEST_URI} =~ m#^/api/*#
    Require valid-user
</Location>

2/

<Location />
    AuthType Basic
    AuthName "Access"
    AuthUserFile /home/xxxxxx/www/public/.htpasswd
    Require expr %{REQUEST_URI} =~ m#^/api/.*#
    Require valid-user
</Location>

Any idea of why these differences ?

Thanks

1
  • Aside: The regex ^/api/* (as in m#^/api/*#) is not strictly correct (at least I doubt it matches what you think it matches). I think you mean ^/api/.* (as in your second example). However, that last .* is superfluous anyway and is the same as ^/api/. Although these tweaks to the regex won't make any difference to your real problem.
    – MrWhite
    Commented Jan 30, 2018 at 16:20

3 Answers 3

0

I'm not sure why that would work on osx-sierra / Apache 2.4.10, but not seemingly on Debian-jessie / Apache 2.4.29. However, as a workaround you could do this a different way using a negative lookahead on a <LocationMatch> container instead of using Apache 2.4 expressions. For example:

<LocationMatch "^(?!/api/.*$).*$">
    AuthType Basic
    AuthName "Access"
    AuthUserFile /Users/xxxxxx/www/public/.htpasswd
    Require valid-user
</LocationMatch>

Now, the directives inside the <LocationMatch> container are only processed when the URL does not start /api/. (This also works on Apache 2.2)

1
  • Thanks but it does not work either ... so I have a problem in my vhost I think ... I post it below ...
    – Nicolo
    Commented Jan 31, 2018 at 9:01
0

This is the complet virtualhost where directives do not work :

<VirtualHost *:80>
DocumentRoot /home/xxxxxx/www/public
ServerName xxxxxx.com

Header set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, PUT, GET, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "x-so-resource, x-so-apikey, x-so-apisecret, x-token, Authorization, Content-Type"

<Directory /home/xxxxxx/www/public>
    php_value include_path "/home/xxxxxx/libs/ZendFramework-1.11.11/library"
    Options +FollowSymLinks 
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

ServerSignature Off

AddDefaultCharset UTF-8

php_value short_open_tag 0

SetEnv APPLICATION_ENV development


<Location />
    RewriteEngine On     
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</Location>


<LocationMatch "^(?!/api/.*$).*$">
    AuthType Basic
    AuthName "Access"
    AuthUserFile /home/xxxxxx/www/public/.htpasswd
    Require valid-user
</LocationMatch>

ErrorLog /var/log/apache2/xxxxxxxx-error.log
CustomLog /var/log/apache2/xxxxxxxx-access.log combined

0

Why complicate it so much instead of doing it straightforward?

<Location />
    AuthType Basic
    AuthName "Access"
    AuthUserFile /home/xxxxxx/www/public/.htpasswd
    Require valid-user
</Location>

<Location /api>
        Require all granted
</Location>

Also note, your previous Directory (the documentroot directory) directive mixing 2.2.x and 2.4.x directives could be screwing everything up.

A mess:

Order allow,deny
Allow from all
Require all granted

The correct thing to do:

Require all granted
  • Be sure to remove 2.2. directives and unload mod_access_compat to be sure, what you do is a recipe for getting unexpected results. Also consider .htaccess files you may have could be affecting the result due to setting AllowOverride all, which ideally you shouldn't use if you are the admin of the site.
2
  • Thanks, it works on OSX Sierra but either not on Debian ... Don't understand why ...
    – Nicolo
    Commented Feb 1, 2018 at 8:34
  • it should work in Debian or any other httpd, it just depends if you land in different context, aka different virtualhost, etc. Commented Feb 1, 2018 at 11:55

You must log in to answer this question.

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