Reference · Websites
Exposed .git and .env files: check whether your website serves them, and block dotfiles
Check whether your website serves /.git/config, /.env or other dotfiles with curl and status codes, then block them with Apache DirectoryMatch and FilesMatch rules or an nginx location with deny all, and test the rules.
| Target artifact | Folders and files whose names start with a dot inside a website's document root, such as /.git/, the Git repository, and /.env, environment settings that can include credentials. |
|---|---|
| Default behaviour | The web server sends any file in the document root that no rule refuses. In a test on 9 November 2026, Apache 2.4.58 and nginx 1.24 on Ubuntu 24.04 answered 200 for /.git/config and /.env with no extra rules. |
| Safe state | No .git folder or .env file in the document root; /.git/config, /.git/HEAD and /.env answer 403 or 404 from outside, on every host name; /.well-known/ still reachable for certificate renewals. |
| Check on Linux | curl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/.git/config |
| Check on macOS | curl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/.env |
| Check on Windows | curl.exe -sI https://DOMAIN/.git/config, then read the status code on the first line |
| Fix | deploy only the files the site serves, and keep .git and .env outside the document root |
| Fix in Apache | <DirectoryMatch "/\.(?!well-known)"> and <FilesMatch "^\.">, each holding Require all denied |
| Fix in nginx | location ~ /\.(?!well-known) { deny all; } in the server block |
Read the result
| Status | Meaning |
|---|---|
200 | The server sends the file. Run the same address with curl -s to read it: a Git configuration starts with [core]. Treat the code, its history and any credentials in it as exposed |
403 | A rule or the file permissions refuse it |
404 | Not found, or a rule answers 404 |
301 or 302 | A redirect, often to https or to www: test the address it points to |
Some sites answer every address with the same page and a 200. If /.env returns your home page, the file is not exposed; read the content, not just the code.
Test every path that matters, on every host name the site answers on:
for p in .git/config .git/HEAD .env .htpasswd; do printf '%s ' "$p"; curl -s -o /dev/null -w '%{response_code}\n' "https://DOMAIN/$p"; doneWhy it matters
- The
.gitfolder is the repository itself: its object store holds the committed files and their history, andconfigholds the repository's settings. Anyone who can read it can rebuild the code, including any password or key that was ever committed. - Laravel's documentation says a committed
.envfile is a security risk if an intruder gains access to the repository, since any sensitive credentials in it would be exposed. An exposed.gitfolder is that access, and a.envfile in the web folder exposes the credentials directly.
Block dotfiles in Apache
<DirectoryMatch "/\.(?!well-known)">
Require all denied
</DirectoryMatch>
<FilesMatch "^\.">
Require all denied
</FilesMatch><DirectoryMatch>is matched against folder paths on disk and covers the folders that match and the files in them, so/\.catches.gitand everything inside it.<FilesMatch>is matched against a file's own name, so^\.catches.envand.htpasswdin any folder. On its own it does not protect/.git/config, whose own name isconfig: in our test, that address still answered200.(?!well-known)leaves/.well-known/alone. Let's Encrypt's HTTP-01 challenge reads/.well-known/acme-challenge/on port 80.- Put the lines in every site file that serves the folder, the port 80 one included. To cover every site on the server instead, save them as
/etc/apache2/conf-available/block-dotfiles.confand runsudo a2enconf block-dotfiles. - Reload only after
configtestprintsSyntax OK:
sudo apache2ctl configtest
sudo systemctl reload apache2Block dotfiles in nginx
location ~ /\.(?!well-known) {
deny all;
}The ~ makes this a case-sensitive regular expression, matched against the requested address. deny all refuses every client; in our test, nginx answered 403. nginx -t checks the syntax and tries to open the files the configuration refers to; reload only when it reports success:
sudo nginx -t
sudo systemctl reload nginxTest the rules
- Run the loop from Read the result again, from outside: every dot path should answer
403or404. - Check that the home page still answers
200:curl -s -o /dev/null -w '%{response_code}\n' https://DOMAIN/ - If certificates renew through
/.well-known/, put a test file in the document root, shown here as/var/www/SITE, and fetch it over port 80.-Lfollows redirects, as Let's Encrypt does. A200means the exception works; a403means the rule also blocks the challenge folder:
sudo mkdir -p /var/www/SITE/.well-known/acme-challenge
echo ok | sudo tee /var/www/SITE/.well-known/acme-challenge/test.txt
curl -sL -o /dev/null -w '%{response_code}\n' http://DOMAIN/.well-known/acme-challenge/test.txt
sudo rm /var/www/SITE/.well-known/acme-challenge/test.txtTroubleshooting
Apache: the whole site answers 403 after adding the rule
The document root itself sits under a folder whose name starts with a dot, such as /var/www/.sites/example, so /\. matches every path. Anchor the pattern at the document root:
<DirectoryMatch "^/var/www/\.sites/example/(.+/)?\.(?!well-known)">
Require all denied
</DirectoryMatch>Apache: /.git/config still answers 200
- A
<Location>block withRequire all grantedoverrides the rule: Apache merges<Location>sections after<DirectoryMatch>and<FilesMatch>. In our test, adding one reopened/.git/configand/.env. - The rule is in the port 80 site file, but the site answers on 443, or the other way round.
- The configuration was not reloaded.
nginx: a dotfile under one folder still answers 200
If the longest matching prefix location has the ^~ modifier, nginx does not check regular expressions, so the deny rule never runs for that folder. Repeat it inside that block:
location ^~ /static/ {
location ~ /\.(?!well-known) {
deny all;
}
}sudo nginx -T tests the configuration and prints every configuration file, which makes such blocks easy to find.
Related guides and lookups
Sources
- Apache HTTP Server: core features, DirectoryMatch, Files and FilesMatch
- Apache HTTP Server: configuration sections and how they merge
- Apache HTTP Server: mod_authz_core, Require all denied
- nginx: ngx_http_core_module, location
- nginx: ngx_http_access_module, deny
- nginx: command-line parameters
- Git: gitrepository-layout
- Laravel: configuration, environment configuration
- Let's Encrypt: challenge types
- Everything curl: write-out
- curl: curl shipped by Microsoft