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.

How to fix6 min read

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 free

Plain-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.

Common questions

How serious is an exposed .env file, really?
Very. It exposes plain-text database passwords, your application key and API secrets, which can mean full access to your data and the ability to spend money through linked services. Our scanner treats it as a critical finding that caps the grade.
I blocked the URL. Is the exposed .env file fix done?
Not yet. You have closed the door, but not undone the damage. If the file was public, assume its contents were read. You still need to rotate every credential it held, because blocking the path does nothing to a secret that is already copied.
Where should the .env file actually live?
Above the public web root, in your application's root folder, not inside the directory your domain serves. Keep the document root pointed at the public folder only, so a browser can never request the file directly.
Does regenerating the application key break anything?
It can. Rotating the app key may invalidate existing encrypted values and active sessions, so users could be logged out and some stored encrypted data may become unreadable. Back up first and plan the change, but still rotate the key if it was exposed.
Can the EB Security scan tell me if someone downloaded my .env?
No. It is an external, passive scan. It can confirm whether the file is publicly reachable right now, but it cannot read your server logs or see who accessed it. For that, check with your host and review your providers' activity logs.
Will the scan also find other exposed files?
Yes. Alongside .env it checks well-known sensitive paths such as /.git/, phpinfo pages, /server-status, config backups and open directory listings. It is a fast first signal, not a replacement for a server-side scan.

How safe is your website?

A free A to E grade in about 10 seconds. No account needed.

Check my site