Blog · 6 min read

Exposed .env and .git files: how to find and fix them

Published

Exposed .env and .git files: how to find and fix them

Some website break-ins need no hacking at all. A file that should have stayed private, such as a configuration file with database passwords or an old backup, sits in a public folder, and anyone who guesses its name can download it. Bots look for these files all day.

This applies to any platform, whether your site runs on WordPress, Laravel, Node, Magento, Shopify-connected apps or custom code, because the mistake is about how files are placed on a server.

Files that should never be public

Why it happens

Usually through habit, not malice. Someone makes a quick backup before an update and forgets it. A deployment copies the whole project folder, including hidden files. A new server skips the rules that block hidden files. Because bots keep probing common file names, an exposed file is often found within days.

Step 1: check what is public

Use a free outside scan, which looks for exposed sensitive files from the visitor's side. You can also test by hand: open your site's address followed by /.env, /.git/config and /backup.zip in a private browser window. You should see a 403 or 404 error, never file contents or a download.

Step 2: remove files that do not belong

Step 3: block hidden and sensitive files

Configure the web server to refuse them. For Apache, add to .htaccess:

<FilesMatch "^\.(?!well-known)|\.(env|sql|bak|log|zip|tar|gz)$">
  Require all denied
</FilesMatch>

For Nginx, add a rule such as location ~ /\.(?!well-known) { deny all; }. Better still, keep the project's real files above the public folder, so only the public files can be requested at all.

Step 4: do not rely on robots.txt

A line in robots.txt asks well-behaved crawlers to stay away, and it even advertises the path. It does not protect anything. Only a server rule or removing the file does.

Step 5: change any secret that was exposed

If a configuration file, backup or .git folder was public even briefly, assume its secrets were copied. Change the database password, API keys and mail credentials, and replace keys in services that used them. Then check whether the site was hacked, review admin logins and turn on two-factor login.

Step 6: make it part of every release

Common questions

Is a .git folder really dangerous?

Yes. Tools can rebuild a site's source code from an exposed .git folder, including old commits that contain passwords.

My site shows a 404 for /.env. Am I safe?

For that file, yes. Also test the other names above, because each is a separate rule.

Does a login page protect my backups?

No. A file in a public folder can be requested directly, without ever visiting your login page.

Can hosted builders like Shopify or Wix have this problem?

Rarely, since you do not manage the server. The risk is higher for any custom app, API or staging site connected to them.

Free tools for this problem

More guides