57

Is it possible to have an .htaccess/.htpasswd access control setup for a given directory, but if they are from a specific IP address, bypass the login/password authentication?

I know you can do something like this in the .htaccess file:

order deny,allow
deny from all
allow from 000.000.000.000

But if you add something along these lines:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/.htpasswd
require valid-user

Then it prompts for the password. Is there any way to do an if/else type setup, or some other solution so that users as a given IP (or set of IPs) don't get prompted for a password, but everyone else does?

2
  • Which apache version are you running? Commented May 2, 2012 at 18:33
  • Anyone have any solutions for Apache 2.2.x? Commented May 3, 2012 at 13:44

6 Answers 6

93

For versions 2.2.X you can use the following...

AuthUserFile /var/www/mysite/.htpasswd
AuthName "Please Log In"
AuthType Basic
require valid-user
Order allow,deny
Allow from xxx.xxx.xxx.xxx
satisfy any

Obviously replace the path to your usersfile and the ip address which you would like to bypass the authentication.

Further explanation of the specifics, can be found at: http://httpd.apache.org/docs/2.2/howto/auth.html

Sign up to request clarification or add additional context in comments.

Comments

26

If you use apache >=2.4, it would be something like this:

<If "%{REMOTE_ADDR} != '127.0.0.1'">
  AuthType Basic
  AuthName "restricted area"
  AuthUserFile /path/to/.htpasswd
  require valid-user
</If>

For more info take a look at the docs.

3 Comments

Unfortunately, looks like the server is running Apache 2.2.19
Works perfectly for me (Apache/2.4.10 (Debian)). Thanks !
You can also use network notation instead of literal addresses: <If "%{REMOTE_ADDR} -ipmatch '192.168.1.0/24'">
17

I am running Apache/2.2.16 (Debian), and had a similar problem, I solved it like this:

(This can be run in both an .htaccess file or directly in the virtualhost under <Location/>)

Order deny,allow
Deny from all
AuthType Basic
AuthUserFile /home/somesite/.htpasswd
AuthName "No entry, unless"
Require Valid-user
Allow from x.x.x.x
Allow from x.x.x.x
Satisfy Any

I allowed entry without password from two different ip, and the rest must enter password to enter.

2 Comments

Thanks a lot, I was trying this inside a <Directory block, which doesn't work, the <Location /> part helped me out a great deal.
works for 2.4 as well, thanks
17

Apache 2.4 compatible:

AuthType Basic
AuthUserFile /www/.htpasswd
AuthName "Protected Area"

<RequireAny>
    Require ip 1.2.3.4
    Require valid-user
</RequireAny>

See the migration guide Upgrading to 2.4 from 2.2 for more examples.

1 Comment

This one works best for me in apache 2.4.x and is a life saver, given that I've a lot of IPs to Require (about 60 ip ranges) o its a way better syntax than the one from NicoMinsk with the if/elseIf/else
7

If you use apache >=2.4, and you want to allow a set of IP, as asked in initial question, you can do it like this :

   <If "-R '192.168.0.0/24'">
            Require all granted
    </If>
    <ElseIf "-R '192.168.1.0/24'">
            Require all granted
    </ElseIf>
    <Else>
            AuthType Basic
            AuthName "restricted area"
            AuthUserFile /etc/apache2/.htpasswd
            require valid-user
    </Else>

Comments

2

In addition to the answer of j5Dev:

# Interne IP-Adressen
SetEnvIf Remote_Addr "^127\.0\.0\.1$" IsIntern
SetEnvIf Remote_Addr "^192\.168" IsIntern
# .. add more IP addresses or ranges here

# Authentication, wenn nicht intern
AuthUserFile /path/to/.htpasswd
AuthName "restricted area"
AuthType Basic
require valid-user
Order allow,deny
Allow from env=IsIntern
satisfy any

1 Comment

None of the answers on this page work for me with the "satisfy any" directive, which is probably pretty important :) Is there anything else that needs configuring? I am on Apache 2.2.22.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.