Search code examples
.htaccessapache2apache2.4.htpasswd

Require login only for certain IP htaccess Apache2


I have an IP, eg 0.0.0.0, which I want to blacklist, so that to access the web server it must enter a username and password. I still want anyone without the IP address of 0.0.0.0 to be able to access the webserver without any checks.

How can I achieve this?

Currently, I have the code to require login for all and to just deny for one IP. I'm not aware of some sort of IF statement which would allow me to integrate the two.

#   Require Auth
AuthUserFile /var/www/example/.htpasswd
AuthType Basic
AuthName "You must be authorised to access this service."
require valid-user

# Block IP
deny from 0.0.0.0

Solution

  • I found a solution:

    <If "%{REMOTE_ADDR} == '0.0.0.0'">
            #   Require Auth
            AuthUserFile /var/www/example/.htpasswd
            AuthType Basic
            AuthName "You must be authorised to access this service."
            require valid-user
    </If>