Apache 2.2 / 2.4 – Allow or Block IP in htaccess file

Allow IP

Apache 2.4+

Require ip 1.1.1.1
Require ip 2.2.2.2

# Behind a loadbalancer

SetEnvIF X-Forwarded-For "1.1.1.1" AllowIP
SetEnvIF X-Forwarded-For "2.2.2.2" AllowIP

Require env AllowIP

Apache 2.2

Order deny,allow
Deny from all
Allow from 1.1.1.1
Allow from 2.2.2.2

# Behind a loadbalancer

SetEnvIf X-FORWARDED-FOR 1.1.1.1 allow
SetEnvIf X-FORWARDED-FOR 2.2.2.2 allow
Order deny,allow
Deny from all
Allow from env=allow

Block IP

Apache 2.4+

Require all granted
Require not ip 111.111.111.111
Require not ip 222.222.222.222
Require not ip 123.123.123.123

Apache 2.2

Order Allow,Deny
Allow from all
Deny from 111.111.111.111
Deny from 222.222.222.222
Deny from 123.123.123.123