WORDPRESS · SEGURANCA
Protecting wp-config.php with .htaccess: what I wrote in 2012 and what has changed
The 2012 rules still block wp-config.php, but they use Apache 2.2 syntax, the .htaccess rule had a regular expression bug and backup copies were left out.
In September 2012 I posted two .htaccess rules for WordPress here: one to protect .htaccess itself and
another to block access to wp-config.php, the file with the database username and password. They still do the
main job, but today I would write them differently.
What the block protects against
On a working server, anyone who opens yoursite.com/wp-config.php in the browser sees a blank page: PHP runs
the file, and it prints nothing. The danger shows up when PHP stops being executed for some reason, such as an
update that breaks the module or a wrong setting. Then the server delivers the file as text, database password
included. The .htaccess block covers that case.
The 2012 rules
<Files ~ "^.*.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>
<files wp-config.php>
order allow,deny
deny from all
</files>
Three problems, looking at them today:
orderanddeny frombelong to Apache 2.2. In 2.4, released in 2012, these directives only work if themod_access_compatcompatibility module is loaded. The current form isRequire all denied.- The first rule’s expression has an unescaped dot. It ends up blocking any file with “hta” in its name
after the first character, not just
.htaccess. And it wasn’t even needed: Apache’s default configuration already denies access to.ht*files. - Copies were left out.
<files wp-config.php>only matches the exact name. Awp-config.php.bak,wp-config.php.oldor thewp-config.php~some editors leave behind isn’t executed as PHP: the server delivers it as text, and that file wasn’t protected.
How I would write it today
<FilesMatch "^wp-config\.php">
Require all denied
</FilesMatch>
The expression starts with wp-config.php and has no fixed ending, so it catches the file and the copies that
start with that name. To check, ask for the headers with curl:
curl -I https://yoursite.com/wp-config.php
The response should be 403 Forbidden. Before the rule, the usual answer is 200 with an empty body.
What .htaccess doesn’t solve
- Nginx doesn’t read
.htaccess. On an Nginx server, none of these rules apply; the block goes in the site configuration, in alocationblock. - Backups with another name. A
wp-config-old.phpor a.zipof the whole site in the public folder isn’t covered by the rule. Better not to leave copies there. - You can take the file out of the public folder. WordPress also looks for
wp-config.phpone folder above the installation. If the host allows it, moving it outsidepublic_htmlsolves the problem at the root. - Permissions. The WordPress documentation suggests 440 or 400 for
wp-config.php. That only works if PHP runs as the same user who owns the file; otherwise the site can no longer read its own configuration.
References
- Hardening WordPress: securing wp-config.php
- Require directive, Apache documentation
- Upgrading from Apache 2.2 to 2.4: access control
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.