.env file exposed on my website: how to fix it
Short answer
If yoursite.com/.env returns your settings, treat every value in it as stolen: block access to dot files at the web server, point the web root at a public folder instead of the project folder, and rotate every secret in the file. Closing the door matters less than changing the passwords and keys, because someone may already have read them.
Why you got this
A .env file holds database passwords, API keys and signing secrets. It becomes downloadable when the web server's document root is the project folder rather than the public subfolder, or when a deploy copies everything to a static host. A request for /.env then gets the file as plain text.
Scanners try /.env on every site they can find, so an exposed file is usually discovered fast. It is common with PHP and Laravel on shared hosting, Node apps served from the repository root and Docker setups that copy the whole folder into the web directory.
What to do now
Open yoursite.com/.env in a private window. If you see text, the file is public. Delete it from the web directory or block it right now, using the rules below.
Rotate every secret that was in the file: database passwords, API keys for payment and AI providers, mail credentials, app keys and signing secrets. Update the server with the new values.
Check provider logs and your database for activity you do not recognise, and look at recent logins and new users.
Also request /.env.local, /.env.production, /.env.bak and .env~ in a private window, because backups are exposed just as often.
How to fix it properly
Deny every file whose name starts with a dot, except .well-known, in nginx.
location ~ /\.(?!well-known) {
deny all;
return 404;
}The Apache equivalent, in the site config or an .htaccess file.
<FilesMatch "^\.(?!well-known)">
Require all denied
</FilesMatch>The real fix is structural. Set the document root to the public folder only (public in Laravel, the build output in a static site), so the .env file is not inside the served directory at all. On hosts with environment-variable settings, use those and keep no .env file on the server.
Add .env to .gitignore and .dockerignore, and keep a .env.example with empty values for teammates.
How to check it is fixed
Request /.env and its common variants again; each should return 404 or 403, not content. Then repeat the request in a private window and from another network.
The verified ScanAnt scan, which runs only on sites you have proved you own, requests common sensitive paths such as .env files, git folders and backups, and reports the ones that answer with real content. It does not store file content. The free quick scan covers only what every visitor already receives, so it can miss files that are not linked anywhere.
Questions
Is blocking the file enough?
No. If it was public, assume it was read. Rotate the secrets inside it.
Why does /.env work only on my server?
It depends on the document root and web server rules. Check where your root points and whether a rule serves dot files.
Does this affect Vercel or Netlify?
Not usually, because those platforms store environment variables separately. A .env committed into your public or static output folder can still be served, so check that too.
What about .env.example?
It is fine if it holds only placeholders. Never put real values in it.
Related checks
Check your site after the fix
The quick scan is free for any public site and needs no signup. Checks that touch your database or files run only after you prove you own the site.
Choose a scan level →