How to fix a public .env file before someone reads your secrets
The exposed .env file fix is two jobs, not one: block public access to the path right now, then rotate every credential the file held. A readable .env hands over your database password, application key and API secrets in plain text, so stopping access closes the door, but anything already served must be treated as copied and replaced.
A .env file is meant to sit on the server and never be sent to a browser. When it ends up reachable at a public URL, usually after a deploy drops it inside the web root or a server is misconfigured, anyone who guesses the path can read it.
The immediate block takes minutes, and the EB Security scanner flags this exact problem in about ten seconds, so you can confirm whether the path is still public before and after you fix it.
Why a public .env is a critical problem
A .env file holds the keys to your whole application in plain text. There is no hashing or encryption: open the URL and the values are simply there to read. That is why our scanner hard-caps the grade when it finds an exposed secret, the same way it does for a broken or untrusted certificate, no working HTTPS, or a Safe Browsing hit.
Depending on the app, a leaked .env can give an attacker a direct route into your database, the ability to forge signed sessions or decrypt data, and working keys to spend money through the third-party services you have connected.
- Database credentials (DB_HOST, DB_USERNAME, DB_PASSWORD): often a direct path into all your customer data.
- The application key (APP_KEY or similar): can let an attacker forge signed cookies and tamper with encrypted values.
- API secrets (payment, email, cloud storage, SMS): usable to send mail as you, run up charges, or read files in your storage buckets.
- Mail and SMTP passwords: a ready-made channel for spoofed email sent from your own domain.
Step 1: block the path immediately
Before anything else, stop the file being served. The exact fix depends on your server, but the goal is the same: any request for /.env (and dotfiles in general) should return 403 or 404, not the file contents.
On Apache, add a rule to your .htaccess or vhost. On Nginx, add a location block and reload. If you cannot edit the config right now, temporarily renaming or moving the file off the public path is a valid stopgap until you do the proper fix in step 2.
- Apache: deny access to dotfiles with a FilesMatch rule for "^\." (or specifically .env) returning 403.
- Nginx: a location block matching /\. that returns 404, then reload the service.
- Confirm by loading https://yourdomain/.env yourself: you should not see the contents.
- Check related leaks the scanner also looks for, such as /.git/, phpinfo pages, /server-status and config backups.
Step 2: move secrets above the web root
Blocking the URL is a patch. The real fix is making sure the file can never be reached by a browser in the first place, which means it should not live in the public directory at all.
Most frameworks expect .env one level above the public web root (the folder your domain actually serves). Point your document root at the public folder only, and keep .env in the application root above it. After moving anything, redeploy cleanly so a stray copy is not left behind in a served directory.
Free, instant, no account
See your site's security grade in seconds.
Free, instant, and no account needed to see your grade.
Scan my site freePlain-English A to E grade in about 10 seconds. No account needed to see it.
Step 3: rotate every exposed credential
This is the step people skip, and it is the one that matters most. If a .env was public, assume every value in it has been read and copied. Blocking the path does nothing about a secret that is already out.
Work through the file line by line and replace each live credential at its source. Do not reuse old values.
- Change the database password, and any other database user passwords.
- Regenerate the application key, and plan for the side effects: rotating it can invalidate existing encrypted data and active sessions.
- Roll every API key and secret in the provider's own dashboard, then update .env with the new values.
- Reset mail and SMTP passwords and any third-party tokens.
- Review provider activity logs for anything unexpected since the file was first exposed.
What the EB Security scan can and cannot tell you
Our free check is an external, passive scan. It reads only what your site already serves publicly, so it can confirm whether /.env (and a small set of other sensitive paths) is reachable right now, and it flags that as a critical finding with a plain-English fix.
What it cannot do is see inside your server. It does not log in, it does not scan your files for malware, and it cannot tell whether anyone already downloaded the file. A clean external result means the path is no longer public; it does not prove a past leak went unused. That is exactly why the credential rotation in step 3 is not optional.
- It confirms whether the file is publicly served, in about ten seconds, with no login or account.
- It cannot read your server logs or tell you who accessed the file.
- If you suspect a real breach, follow up with your host and a server-side scan, and treat rotation as mandatory.
A public .env is critical because it leaks plain-text secrets. Block the path, move the file above the web root, and rotate every credential it held, because anything exposed should be treated as already stolen.