Imagine that your WordPress website is a digital castle. You have walls (firewalls), guards (secure passwords) and a moat (security plugins). But what would happen if you left the master keys to the castle, the ones that open the treasure room and the king's chambers, out in the open for everyone to see? That's exactly what happens when you leave your archive wp-config.php without proper protection. This small file is the heart of your WordPress installation, and protecting it is not an option, it is a critical necessity for the security of your website. In this complete guide, we'll show you step-by-step how to protect the wp-config.php file using proven and effective methods, from the simplest to the most advanced.
Why is it Absolutely Critical to Protect the wp-config.php File?
Before we dive into protection techniques, it is critical to understand what makes this file so valuable to you and so coveted by attackers. The file wp-config.php is not just any file; it is the nerve center that connects your WordPress with its database, the place where all your content is stored.
According to WordPress security experts such as the teams at Webempresa and Pablo Cianes, This file contains the most sensitive information of your site. Unauthorized access to it would expose:
- Database Credentials: The database name, user, password and server host. With this data, an attacker could access, modify or delete all your information.
- Unique Authentication Keys (Security Keys and Salts): These keys encrypt cookies and other sensitive user session information. If compromised, an attacker could hijack user sessions, including that of the administrator.
- Database Tables Prefix: Knowing this prefix facilitates targeted SQL injection attacks.
In summary: Whoever controls your wp-config.php, If you don't, you control your website completely. Leaving it exposed is the equivalent of handing the keys to your business to a stranger.
Essential Preparations: Before Touching Anything
Before implementing any of the following security measures, there is one step that you cannot skip under any circumstances.
MAKE A BACKUP COPY! Always, always, always create a full backup of your website before modifying critical files such as wp-config.php o .htaccess. A simple typo could render your site inaccessible. You can use a backup plugin or do it manually through your hosting control panel (cPanel, Plesk, etc.).
To perform the actions in this guide, you will need access to your server files. You can get it through:
- A FTP client like FileZilla.
- The File Manager available in your hosting control panel.
3 Effective Methods to Protect wp-config.php
Now that you understand the importance and have made your backup, it's time to take action. Here are three methods, ordered from simplest to most advanced, to shield your configuration file.
Method 1: The Simplest Solution - Move the File One Level Up
This is one of the most elegant and simple forms of protect your wp-config.php. By default, WordPress looks for this file in the root directory of the installation (typically public_html o www). However, if it does not find it there, it is programmed to automatically search for it in the directory immediately above.
This feature allows us to move the file out of the public directory, making it inaccessible from any web browser, but keeping the site working perfectly. As detailed in this effective protection technique, The process is very simple.
Steps to follow:
- Connect to your server via FTP or use your hosting's File Manager.
- Navigate to the root directory of your WordPress installation (ex:
/public_html/). - Locate the file
wp-config.php. - Click and drag it (or use the «Move» function) to the directory one level above. For example, if you were in
/home/user/public_html/, move it to/home/user/. - That's it! Refresh your website to confirm that it is still working properly.
This method is highly recommended for its simplicity and effectiveness. It hides the file from public view without the need to edit any code.
Method 2: Block Direct Access with .htaccess file
If for some reason you can't or don't want to move the file wp-config.php, The next most popular method is to use the file .htaccess for deny public access to it. The file .htaccess is an Apache server configuration file that allows you to set specific rules for your directory.
By adding a few lines of code, we can instruct the server to block any attempt to access wp-config.php from a browser.
Steps to follow:
- Connect to your server and browse to the root of your WordPress (the same place where the
wp-config.php). - Locate the file
.htaccess. (Note: files beginning with a dot may be hidden. Make sure you have the «Show hidden files» option enabled in your FTP client or File Manager). - Open the file for editing.
- Add the following code, preferably at the beginning of the file, before the WordPress rules:
order allow,deny
deny from all- Save the changes and close the file. To verify this, try accessing
tusitio.com/wp-config.php. You should receive a 403 (Forbidden) or similar error.
Attention! One Key Server Requirement for this Method
For this method to work, your Apache server configuration must allow it. Specifically, as they point out at advanced WordPress tutorials, the board of directors AllowOverride in your server configuration you must have enabled the option Limit. Otherwise, the server will ignore the rules you have added in .htaccess, and the file will still be unprotected. If after applying the code you are still able to access the file (or download a blank version), contact your hosting provider and ask them if AllowOverride Limit is activated for your account.
Method 3 (Advanced): Customized relocation with require_once
This method offers you maximum flexibility and security, allowing you to move the configuration file to virtually anywhere on your server that is not publicly accessible. It is ideal for custom server configurations or for those seeking a paranoid level of security.
The idea is to move the real file and leave in its place an «imposter» that simply tells WordPress where to find the real one. This method is another of the solutions explored by WordPress fortification experts.
Steps to follow:
- Choose a secure, non-public location on your server to save the file. For example, you could create a directory called
/wp_configurations/in your home directory (e.g:/home/user/configurations_wp/). - Move your file
wp-config.phpto this new location. - Now, in the root of your WordPress (where the original file was), create a new file called
wp-config.php. - Edit this new file and paste the following code into it:
<?php
/** Carga el verdadero archivo de configuración desde una ubicación segura. */
require_once('/home/tu_usuario/configuraciones_wp/wp-config.php');
?>- VERY IMPORTANT: Replaces
/home/tu_user/configurations_wp/wp-config.phpwith the actual absolute path where you moved your file. If you don't know the absolute path, you can ask your hosting provider or use a PHP function likeecho getcwd();in a temporary file to find out. - Save this new file. WordPress will now load your configuration from the safe location you have defined.
Which Method Should I Choose to Protect my wp-config.php?
With three options on the table, you may be wondering which one is best for you. Here's a quick recommendation:
- For most users (beginners and intermediate): The Method 2 (using .htaccess) is a gold standard. It's fast, effective and widely supported. If it works with your hosting, it's an excellent first choice.
- If Method 2 does not work or you are looking for maximum simplicity: The Method 1 (move one level up) is your best ally. It is incredibly easy to implement and very secure.
- For developers and system administrators: The Method 3 (relocation with require_once) offers the greatest control and is perfect for complex environments or when maximum obfuscation is sought.
Frequently Asked Questions (FAQ) about the Protection of wp-config.php
Will protecting wp-config.php affect WordPress updates?
No. None of the methods described interfere with the WordPress update process, as the WordPress core knows how to locate the file (Method 1 and 3) or the server rules (Method 2) do not affect its internal operations.
Can I combine these methods for even greater security?
Yes! In fact, it is an excellent practice. You can move the file up one level (Method 1) and also add the deny rule in your .htaccess (Method 2). This creates a layered defense: if one fails, the other continues to protect your file.
Combining Method 1 and Method 2 is a robust and recommended security strategy for double-layer protection.
I made a mistake and now my site shows «Error establishing a connection to the database». What do I do?
Relax, this is the time to use the backup you made. This error almost always means that WordPress can't find or read the file wp-config.php. Restore the file to its original location and state from your backup. Then, review the steps carefully. If you used Method 3, the most common error is an incorrect absolute path in the file. require_once.
Conclusion: Your Next Step Towards an Unassailable Website
You've reached the end of this guide, and now you have the knowledge you need to close one of the most critical security holes in any WordPress site. Protecting wp-config.php file is not a technical luxury, it is a fundamental pillar of a sound security strategy.
Either by moving the file, blocking access with .htaccess or relocating it to a custom path, the action you take today will significantly strengthen your digital castle. Don't leave the security of your website to chance. Take the next 15 minutes, choose the method that works best for you and implement it. Your peace of mind and the integrity of your website will thank you.
Do you need help with this?
We secure your WordPress site against attacks: security audit, comprehensive hardening, and continuous monitoring. Discover our WordPress security and hardening service.