If your .env file is publicly accessible through Apache, block it immediately. If it contains database passwords, API keys, JWT secrets, etc., assume those credentials are compromised and rotate them.
1. Add this to your Apache virtual host
<FilesMatch "^\.env">
Require all denied
</FilesMatch>
This blocks .env, .env.local, .env.production, etc. Apache 2.4 supports Require all denied for authorization. Apache HTTP Server
For example:
<VirtualHost *:443>
ServerName your-domain.com
DocumentRoot /var/www/html
<FilesMatch "^\.env">
Require all denied
</FilesMatch>
# your existing configuration...
</VirtualHost>
Then:
apachectl configtest
systemctl reload apache2
2. Test it
From another machine or the server:
curl -I https://your-domain.com/.env
You want:
HTTP/1.1 403 Forbidden
Also test common variants:
curl -I https://your-domain.com/.env.local
curl -I https://your-domain.com/.env.production
curl -I https://your-domain.com/.env.example
3. Better solution — move .env outside the web root
If your application is:
/var/www/html/
don't keep:
/var/www/html/.env
Instead:
/var/www/.env
and configure your application to read:
/var/www/.env
That makes the file impossible to request through the web server.
4. Check whether it has already been exposed
If you just discovered this, check Apache's access log:
grep -E 'GET /(\.env|\.env\.)' /var/log/apache2/access.log
Also search rotated logs:
zgrep -hE 'GET /(\.env|\.env\.)' /var/log/apache2/access.log* 2>/dev/null
If the .env contained passwords or keys and there are hits in the log, rotate those credentials.
If you show me your Apache VirtualHost configuration (/etc/apache2/sites-enabled/*.conf), I can tell you exactly where to put the block without guessing your domain.