Apache http whitelist multiple IP Addresses

Here's a way to whitelist as many IP addresses as you want, using Apache rewrite module.

In the VirtualHost add this:

<ifmodule mod_rewrite.c>
  RewriteEngine On
  RewriteMap ips txt:/etc/apache2/allowed_ips
  RewriteCond ${ips:%{HTTP:X-Forwarded-For}|NOT-FOUND} =NOT-FOUND
  RewriteCond ${ips:%{REMOTE_ADDR}|NOT-FOUND} =NOT-FOUND
  RewriteRule ^(.*)$ https://google.com [R,L]

And this file is where you add the allowed IP addresses: /etc/apache2/allowed_ips

Contents: - -

Any other IP addresses trying to access your apache website will be redirected to google, or you can put a forbidden page there too. Just to note this setup is flexible enough to use a load balancer (X-Forwarded-For) or straight to the server (REMOTE_ADDR), no modification needed.