Protect Your WordPress WP-Config So You Don’t Get Hacked
Today while at work I was browsing my feeds when I stumbled across a very odd headline: You got h4ck3d!
I thought it was a joke. So I went to the website.

As you can see from the image, the hack is legit. The author promptly removed the post within a few hours and it was like nothing ever had happened.
The way the hacker got in was through the "wp-config.php" when it was readable as plain text. From that, the hacker can get your database name, and your database username and password. This could've have easily been prevented, even if the hacker could read the "wp-config" file.
Protect it the .htaccess Way
Josiah Cole wrote a nice htaccess tutorial on modifying your .htaccess to protect the wp-config.
Here's the code he used:
-
# protect wpconfig.php
-
<files wp-config.php>
-
order allow,deny
-
deny from all
-
</files>
Protect the WP-Config by Moving the File
Now one can move the wp-config to an unpredictable location and change the code in the source, but that would be a pain to do with every WordPress upgrade.
How about creating a separate PHP file in a non-WWW accessible location and use the WP-Config to include that file.
Say for example that your web include path for your server was /home/yourname/public_html/. You can actually save a file in the /home/yourname/ area and it won't be web accessible. Meaning that even if somebody were able to read your wp-config, they wouldn't get anything valuable.
Here are the steps that I took.
Create a "config.php"
Within this config.php file I included the following:
-
<?php
-
-
// You can have multiple installations in one database if you give each a unique prefix
-
$table_prefix = 'yourdbprefix_'; // Only numbers, letters, and underscores please!
-
?>
I uploaded this file to a non-WWW readable location. Normally this should be the directory before "public_html" or "www".
Modify the WP-Config
I then modified the "wp-config.php" file to include the file. If somebody were to somehow read the contents of my WP-Config, all they would see is this:
-
<?php
-
include('/home/yourname/config.php');
-
-
// Change this to localize WordPress. A corresponding MO file for the
-
// chosen language must be installed to wp-includes/languages.
-
// For example, install de.mo to wp-includes/languages and set WPLANG to 'de'
-
// to enable German language support.
-
-
/* That's all, stop editing! Happy blogging. */
-
-
require_once(ABSPATH.'wp-settings.php');
-
?>
Please note that the include paths change from server to server, but hopefully you get the idea. Save your sensitive information in a non-WWW location, and have the WP-Config file read it in. This way you won't have to change anything if you have to upgrade WordPress.
Conclusion
If a person with malicious intent finds your WP-Config file and can actually read the contents, your website is exposed. Devlounge wrote an article earlier today that revealed how easy it is for a hacker to change your password (and get admin access to your blog) using phpMyAdmin.
You can never be too careful about these things, so protect your WP-Config and make sure you have a recent database backup.
If there are any more ways to protect the WP-Config that I didn't already mention, please feel free to add them in the comments.
Ronald is frequently found laying his thoughts out in strong, straight-forward articles on various web related topics. He is the author of the popular WordPress plugin
First time here, or frequent flyer. Whatever the case may be, we highly recommend 


You can simply change the file permissions to local only, users get a 403 error.
Found on Stumble, excellent info. Thanks
thanks, excellent info!
Thanks for the tip, I will try this as for double protection.
Wow that’s scary. I’ma change mine right away!!
Wow…that is really scary. I’m going to try and protect it the .htaccess way. Seems like the easiest and a safe way. Thanks for the writeup!
David
Not using WP at the moment, but I’ve set up a few. This is really good for everyone out there to remember.
Don’t depend on Apache to always work with PHP, if it doesn’t it will send the file in pure text.