Protect wp-config.php and xmlrpc.php via .htaccess

Protect wp-config.php and xmlrpc.php with .htaccess

wp-config.php is the most sensitive file of a WordPress site: the database name, user and password in plain text, the authentication keys and salts, the table prefix. Normally PHP executes the file and a direct request returns an empty page. But that depends on PHP working: if the PHP handler breaks after a server update, Apache may return the file as plain text. A second, independent layer of protection costs a few lines in .htaccess.

xmlrpc.php is another frequent target: it is a second login door that a custom login URL or a captcha on the login page does not cover, and its pingbacks are abused to flood other sites. If you do not use XML-RPC (old mobile apps, some Jetpack features and remote publishing tools need it), it is safer to close it.

Problem: Deny direct HTTP access to wp-config.php and xmlrpc.php on an Apache server.

Solution: <Files> blocks in the .htaccess file in the WordPress root. Apache rejects the request before PHP is involved.

Apache 2.4

Add to .htaccess, outside the # BEGIN WordPress … # END WordPress block (WordPress rewrites that block when the permalinks are saved):

# Deny direct access to wp-config.php
<Files wp-config.php>
    Require all denied
</Files>

# Deny xmlrpc.php if you do not use it
<Files xmlrpc.php>
    Require all denied
</Files>

Apache 2.2

Old servers do not know Require, the same rules look like this:

<Files wp-config.php>
    Order allow,deny
    Deny from all
</Files>

<Files xmlrpc.php>
    Order allow,deny
    Deny from all
</Files>

Check

curl -I https://example.com/wp-config.php
curl -I https://example.com/xmlrpc.php

Both must return 403 Forbidden. Then open the site and the admin to make sure everything works: a mistake in .htaccess gives a 500 error on every page.

NOTE: The .htaccess file itself is usually already protected: the default Apache configuration denies all files starting with .ht. On Nginx .htaccess is not read at all, so the same is done with location = /xmlrpc.php { deny all; } and location = /wp-config.php { deny all; } in the server block.