Apache – Access based on an IP or Username/password

Recently I came along the need of having access to some intranet site based on IP or if the IP was outside the LAN apache should prompt for a username/password.

It took me some time to figure out, but this can be done by the satisfy
option. So I now have in my apache-config the next configuration:

<Directory "/usr/local/www/intranet">
  Options Indexes FollowSymlinks MultiViews
  AllowOverride None
  order deny,allow
  deny from all
  # Allow LAN Location A
  allow from
  # Allow LAN Location B
  allow from
  # Allow VPN-subnet
  allow from

  # Username/password request
  AuthType Basic
  AuthName "Example.Com Intranet"
  AuthUserFile /usr/local/etc/intranet/webusers.pwl
  require valid-user

  # Allow or require must be satisfied
  Satisfy any

And it is working well, if you're from outside the defined subnets... you need to enter your username/password.