In my Apache, I have a long list of IPs to be blocked (for some reasons). And then I'm currently putting those rules inside the .htaccess
files of the each and every single docroot (domains) I have.
The rules look like this:
SetEnvIF X-Forwarded-For "1.2.3.*" DenyIP
SetEnvIF X-Forwarded-For "100.200.*.*" DenyIP
..
..
..
..
..
..
Order Deny,Allow
Deny from env=DenyIP
These same .htaccess
files are now located like:
/var/www/html/www.site-a.com/.htaccess
/var/www/html/www.site-b.com/.htaccess
/var/www/html/www.site-c.com/.htaccess
/var/www/html/www.site-d.com/.htaccess
/var/www/html/www.site-e.com/.htaccess
/var/www/html/www.site-f.com/.htaccess
/var/www/html/www.site-g.com/.htaccess
..
It works that way. But then whenever I have to update the IPs, I have to edit in all of these files.
Question:
How do I apply these common rules Server-wide (Apache-wide), so that they are applied on every single websites docroots (domains) I have in my Apache?
Take a look at Apache 2.2 Configuration Sections and Access Control.
Allow
andDeny
belongs to context:Directory
and.htaccess
..htaccess
is a in-place equivalent for<Directory>
, but with limitations, e.g.Include
inside.htaccess
.AllowOverride
controls what you can change with your.htaccess
configuration.Configuration sections have merging order, shortened:
You can have global
<Directory>
sections outside<VirtualHost>
sections:In short, inside your main
httpd.conf
, have this and don't override it in<VirtualHost>
:If you don't want to have the long
SetEnvIF
list inhttpd.conf
, use [Include][6]
:Notice what all the documentation linked here mentions:
Access Control in Apache 2.4 is a bit different, but you'll be able to use environment variables with its mod_authz_core
Require
env
. As you set the variable for deny instead of allow, you'd need to usenot
before theenv
entity-name.