Service files must not be accessible through a browser. If an attacker downloads .env, .git, composer.json, a backup, or a log file, they may discover the project structure, dependency versions, file paths, API keys, passwords, and database connection details.
WordPress, Elementor, and installed plugins do not protect the server against incorrectly configured web access. Access restrictions must be configured at the Apache or Nginx level, and unnecessary files should be removed from the web root. Below is a practical sequence of steps, configuration examples, and a way to verify the result.
Which Files and Directories Should Be Blocked
The most dangerous objects are those containing configuration, source code, change history, backups, or logs. Even if a file does not contain a password directly, it may reveal component versions and paths to important resources.
Pay particular attention to .env and related files such as .env.production, the .git directory, Composer and Node.js files, database dumps, archives, and logs. In WordPress, you should also avoid leaving temporary copies of wp-config.php, old configurations, or files created during migration in the public directory.
- .env, .env.local, .env.production, and other files containing environment variables.
- .git, .svn, and other version-control system directories.
- composer.json, composer.lock, package.json, package-lock.json, and yarn.lock.
- .sql, .log, .bak, .old, .orig, and .swp files, as well as .zip, .tar, and .gz archives.
- Website, database, and configuration backups if they are stored under the web root.
- Files containing keys, certificates, and private settings that do not need to be served directly to a browser.
Check the Web Root and Backups Before Configuration
The web root is the directory from which the web server serves the website files. For WordPress, it may be public_html, public, www, or another directory defined in the domain configuration. Check not only the site root, but also nested theme, plugin, and uploads directories.
First, remove or move outside the web root any files the site does not need. Backups and logs should preferably be stored in a separate directory that is inaccessible to the web server, or in external storage. Blocking HTTP access does not replace removing unnecessary data: the file may still be accessible to another process or accidentally copied during a migration.
Also check file-system permissions separately. Blocking downloads through Nginx or Apache is important, but excessive file-system permissions can create other risks.
- Find archives, database dumps, logs, and old configuration copies.
- Move .git outside the public directory if it is required for deployment.
- Do not store passwords or API keys in files that must be served to a browser.
- Make sure a backup does not have a predictable URL in the public directory.
How to Block Service Files in Apache
For Apache, rules can be added to the virtual host configuration or, if the hosting provider allows it, to .htaccess in the site root. The virtual host configuration is generally preferable: it is centralized, easier to administer, and does not depend on AllowOverride being permitted.
The example below blocks hidden files, specific service configurations, and common backups. The exception for .well-known is necessary so that standard domain-verification files are not affected, for example when issuing a certificate.
Adapt the rules to your project structure. Do not mechanically block every dotfile if the site or another service legitimately uses a hidden file in the public directory.
- After changing the configuration, check the Apache syntax before reloading it.
- If you use .htaccess, make sure the server allows the rewrite module and processing of the relevant directives.
- Do not insert .htaccess rules into an Nginx configuration: they are different configuration formats.
<IfModule mod_rewrite.c>
RewriteEngine On
# Hidden files and service directories, except ACME/.well-known
RewriteRule (^|/)\.(?!well-known(?:/|$)) - [F,L]
# Specific configuration and dependency files
RewriteRule (^|/)(?:composer\.(?:json|lock)|package(?:-lock)?\.json|yarn\.lock)$ - [F,L]
# Backups, dumps, and logs
RewriteRule (^|/).+\.(?:bak|old|orig|sql|log|zip|tar|gz)$ - [F,L]
</IfModule>
<FilesMatch "^(\.env(?:\..*)?|wp-config\.php(?:\..*)?|composer\.(?:json|lock)|package(?:-lock)?\.json|yarn\.lock)$">
Require all denied
</FilesMatch>
How to Block Access in Nginx
In Nginx, add the rules to the server block that serves the domain. The regular location for hidden files blocks .git, .env, and other objects whose names begin with a dot. Keep the .well-known exception if it is used for technical domain verification.
A separate block for service files is necessary because not all dangerous files begin with a dot. After making changes, check the configuration with nginx -t and only then reload Nginx.
- Place the rules in the configuration for the specific domain, rather than accidentally adding them to another server block.
- If a CDN or proxy is located in front of Nginx, check the response through it as well: the cache may temporarily return an old result.
- Do not allow access to .git even if the repository supposedly contains no secrets.
location ~* /\.(?!well-known(?:/|$)) {
deny all;
access_log off;
log_not_found off;
}
location ~* ^/(?:composer\.(?:json|lock)|package(?:-lock)?\.json|yarn\.lock|wp-config\.php(?:\..*)?)$ {
deny all;
}
location ~* \.(?:bak|old|orig|sql|log|zip|tar|gz)$ {
deny all;
}
Verify the Result with HTTP Requests
After configuration, check not only the home page but also specific URLs. Use curl or another tool that displays the HTTP status and headers. Run the requests from outside the server, because a local check may bypass a proxy, CDN, or routing rules.
For a protected resource, a normal result is 403 Forbidden or 404 Not Found. The important point is that the response must not contain the file contents, a dump page, a fragment of JSON, or a message containing secret variables.
- Check /.git/HEAD — the response must not contain a line identifying the current branch.
- Check /.env — environment variables and passwords must not be displayed.
- Check /composer.json — the file must not be downloaded directly.
- Check /backup.zip, /database.sql, and common backup filename variations.
- Check similar paths in nested directories if such files could remain in a theme or plugin.
curl -i https://example.com/.git/HEAD
curl -i https://example.com/.env
curl -i https://example.com/composer.json
curl -i https://example.com/backup.zip
# Check that the response contains no secret data
curl -sS https://example.com/.env | head
Common Protection Mistakes
The most common mistake is blocking one known URL instead of an entire file category. An attacker may find a copy under another name or in another directory. Another problem is relying solely on robots.txt. It does not block access and is not a security mechanism.
It is also dangerous to serve service files through PHP or another handler. If the rule for static files does not work, the web server may pass the request onward and the file may be returned as plain text. Check the order of locations in Nginx and the routing rules in Apache.
Do not rely only on a WordPress security plugin. CMS-level protection is useful as an additional layer, but a request for .git or a backup may be processed before WordPress starts.
If a file has already been accessible online, simply blocking it is not enough. Change database passwords, API keys, tokens, and other secrets that may have been exposed. If necessary, review web-server logs and access history.
- Do not use robots.txt as a blocking mechanism.
- Do not leave backups in wp-content/uploads or in the site root.
- Do not disable .well-known checks without understanding the consequences for certificates.
- Do not check only for a 403 status: make sure the response body contains no secrets.
- Do not edit the production configuration without a backup and a rollback plan.
Post-Change and Deployment Checks
Protection of service files should be included in the launch checklist for a WordPress site, as well as in theme, Elementor, plugin, and server-migration updates. After each deployment, check whether new archives, logs, or configuration files have appeared in the web root.
Keep the server configuration in a change-control system, but store the repository itself outside the public directory. Secrets should preferably be passed through environment variables or secure storage rather than committed to Git.
Periodically review logs for requests to .env, .git, .sql, .zip, and other service extensions. A large number of such requests may indicate automated scanning or attempts to find exposed data.
- Add checks for service URLs to post-release tests.
- Make sure the staging environment does not expose configuration files or backups either.
- After a leak, immediately revoke and reissue secrets instead of only changing the access rule.
- Restrict access to administrative and technical tools separately from file blocking.
Frequently asked questions
Should the server return 403 or 404?
Both options are acceptable if the file contents are not disclosed. A 403 clearly indicates that the resource exists, while a 404 conceals the fact that it is present. The choice depends on the server policy, but the important point is not the status code — it is the absence of service data in the response.
Is adding a restriction to .htaccess enough?
It may be sufficient for Apache if the hosting environment allows the required directives and the file is located in the correct directory. However, the virtual host configuration is more reliable. .htaccess does not work with Nginx; the rules must be added to the Nginx configuration.
Can .git be left on the server for automated deployment?
It can, but the directory must be outside the web root or completely blocked at the web-server level. A safer approach is to deploy into a working directory and expose only the directory containing the website files publicly.
What should I do if .env or backup.zip has already been accessible through a browser?
First, block access and remove the file from the web root. Then change all passwords, keys, and tokens it may have contained, check logs and caches, and assess whether the site owner or security team needs to be notified.
Conclusion
Blocking web access to .git, .env, configuration files, logs, and backups is a basic but important level of protection for a WordPress site. Configure the rules in Apache or Nginx, remove unnecessary files from the web root, and check specific URLs over HTTP.
Protection must cover not only WordPress and Elementor, but the entire file structure of the domain. Regular checks after updates and deployments help prevent service files from becoming publicly accessible again.