WordPress wp-config and .htaccess: Hardening Guide 2026
WordPress wp-config hardening is the highest-value security work you can do on a WordPress site in under an hour, because two small files decide how much damage an attacker can do once they find a way in. The first file, wp-config.php, holds the database password, the eight secret keys that sign every login cookie, and the constants that decide whether an administrator can install code from the dashboard. The second file, .htaccess, is the gatekeeper that runs before WordPress itself: it decides which files a visitor may request at all, which folders may execute code, and which endpoints answer. Harden those two files and you have not made your site unhackable, no site is, but you have taken away the easiest paths from a foothold to a takeover: the stolen admin session that pastes a web shell through the built-in editor, the uploaded image that turns out to be PHP, the misconfigured server that serves your database password as plain text.
This guide covers both files end to end, with the exact lines to add and what each one protects against. You will learn what lives in wp-config.php and why it is the first file an attacker looks for; how to set file permissions so only you and the web server can read it; how to replace the eight salts and keys, move the file outside the web root, and delete the stray backup copies that leak it; the seven constants worth setting, including the one that removes the dashboard code editor entirely; and then the .htaccess half: the five rules that do most of the work, the uploads rule that stops a web shell from executing, the xmlrpc decision, the optional IP lock on the login page, and the security headers that cost nothing and break nothing. Every code example is complete and copy-paste ready, and nothing here needs a paid plugin. The only requirements are a backup, a plain text editor, and the discipline to test the site after every change.
The timing is deliberate. This series has spent the last month documenting how WordPress sites actually get attacked: the PHP2Shell remote code execution flaw, the Comment2Shell vulnerability, the critical PHP session flaws fixed in November 2026, plugin vulnerabilities, cross-site scripting, and PHP execution in the uploads folder. Those posts explain how attackers get in. This one closes the doors they walk through after they arrive, and it pairs naturally with the previous two posts in the series: the file upload security guide, which stops malicious files from being uploaded in the first place, and the backup plugin guide, which is what saves you when hardening fails. Read this guide, make a backup first, and then change the two files. In that order.
Quick Takeaways
- WordPress wp-config hardening starts with permissions: set
wp-config.phpto 400 or 440, so only you and the web server can read it. Directories stay 755, ordinary files stay 644. - The eight keys and salts in
wp-config.phpsign every login cookie. Replace them with fresh values from the official WordPress.org generator, or withwp config shuffle-salts, and every logged-in user is signed out, which is exactly what you want after a suspected break-in. - Set
DISALLOW_FILE_EDITto remove the theme and plugin code editors from the dashboard. A stolen admin session can then no longer paste a web shell intofunctions.phpfrom the browser. - Keep debug output off on a live site:
WP_DEBUGfalse,WP_DEBUG_DISPLAYfalse, and errors to a private log file. Error messages shown to visitors leak file paths and server details. - Move
wp-config.phpone directory above the web root when your host allows it, and delete backup copies ending in.bak,.old, or.orig. A backup copy of the file is the file, as far as an attacker is concerned. - In
.htaccess, three rules do most of the work: deny PHP execution inwp-content/uploads, deny direct access towp-config.php, and turn off directory listing withOptions -Indexes. - Block
xmlrpc.phponly if nothing on your site uses it. Blocking it breaks Jetpack and the WordPress mobile apps, so check first. .htaccessonly works on Apache and LiteSpeed. On nginx it is ignored entirely, and the same rules go in the server block instead. This site runs on LiteSpeed, so.htaccessrules apply here.- Always place your own rules outside the
# BEGIN WordPressblock, or WordPress will overwrite them. Back up both files before you edit, and test the home page, the login page, the admin dashboard, media uploads, and every form after each change. A typo inwp-config.phpwhite-screens the site; a wrong directive in.htaccessgives a 500 error on every page. - Hardening is one layer, not a replacement for the rest of the stack. Updates, strong passwords, a backup plugin, and a plugin audit still matter, and no hardening stops a zero-day in a component you never touched.
Why These Two Files Matter More Than Any Plugin
Every WordPress security guide eventually lists thirty settings, and most of them are padding. The reason this guide covers two files instead of thirty settings is that these two files sit at the two points where an attack turns from a nuisance into a takeover. Understanding the two points makes every rule in this guide obvious, so start here and the rest reads as consequences, not as a checklist to memorise.
The first point is the configuration file. wp-config.php is the file WordPress reads before anything else loads. It holds the database name, username, and password, the eight secret keys and salts, the table prefix, and the constants that change how WordPress behaves. If an attacker can read this file, they have the database password, which means they can read and change every post, page, user, and setting directly, without ever logging in to WordPress. If they can write to it, they own the site outright. The file is not normally served to visitors, because PHP executes it instead of printing it, which is why many site owners never think about it. But “normally” is doing a lot of work in that sentence: a server misconfiguration, a failed PHP upgrade, or a backup copy left in the web root can all turn a file that should never be readable into a file that is served as plain text to anyone who asks. The wp-config.php half of this guide exists for that failure case, and for the quieter risk, which is that the file is readable by more accounts on the server than it needs to be.
The second point is the request gate. .htaccess is read by Apache, and by LiteSpeed, on every single request, before WordPress loads a line of code. It is the only place where you can refuse a request for a file without WordPress having to decide anything. That matters because the most common WordPress attacks do not go through WordPress at all: they request a file directly. An uploaded web shell in the uploads folder is only dangerous if the server will execute it when requested. A request for wp-config.php is only dangerous if the server will serve it. A request for xmlrpc.php is only dangerous if the endpoint answers. .htaccess rules answer all three questions before WordPress wakes up, which makes them faster and more reliable than any plugin that tries to do the same job from inside WordPress. A plugin can only block a request after WordPress has loaded, which is after the attacker has already reached the code they wanted to reach.
There is a third reason these two files beat plugins, and it is the least obvious one. Plugins are code, and code has vulnerabilities. Every security plugin you install is another component that can itself be attacked, and the history of WordPress security plugins includes several serious flaws of their own. The two files in this guide are configuration, not code. They cannot be exploited the way a plugin can, they do not need updating, they do not slow the site down, and they cost nothing. That does not mean you should uninstall your security plugin. It means the two files are the foundation, and the plugin is the layer on top. A site with hardened files and no security plugin is safer than a site with a security plugin and default files, because the plugin watches the front door while the files decide what the attacker finds once they are inside.
One more scope note before the rules. This guide is about shrinking the attack surface: reducing what an attacker can reach and what they can do with it. It is not about detecting attacks, logging them, or cleaning up after one. If your site is already hacked, do not start here. Start with the backup and recovery guide from Day 28, restore from a clean backup, close the hole the attacker used, and only then harden these two files. Hardening a hacked site without cleaning it first just locks the attacker inside with you.
Before You Touch Either File
Both files in this guide are load-bearing. A syntax error in wp-config.php produces a white screen, because WordPress cannot start without it. A wrong directive in .htaccess produces a 500 error on every page of the site, because Apache reads the file on every request and refuses to serve anything when it cannot parse it. Neither failure is hard to fix, but both are alarming the first time they happen, and both are entirely avoidable if you follow three habits that take five minutes and save you from every mistake in this guide.
First, make a backup of both files before you edit them, and keep the copies somewhere that is not the site itself. Download wp-config.php and .htaccess to your computer, or copy them to a folder outside the web root. The Day 28 guide covers full-site backups in detail; for this job, the two files are enough, because the only thing you are about to change is the two files. If a change breaks the site, the fix is to put the copy back, which takes seconds. If you have no copy, the fix is to reconstruct the file from memory, which takes much longer and is how a five-minute job becomes an evening. Keep the backup copies off the server, or at least outside the public web folder, because a backup copy of wp-config.php left in the web root is itself a security problem, as the section on stray copies explains below.
Second, edit with a plain text editor, never with a word processor, and never in the dashboard. Word processors insert curly quotes and invisible characters that PHP and Apache both reject. The dashboard file editor, which this guide is about to disable, is the worst place to edit either file, because a mistake there can lock you out of the dashboard itself. Use a code editor such as VS Code, Notepad++, or Sublime Text, or the file manager in your hosting panel, which edits the file on the server directly. If you use the file manager, download a copy first anyway, because the file manager has no undo.
Third, change one thing at a time and test after each change. Edit the permissions, load the site. Add the salts, load the site. Add one .htaccess rule, load the site. The test after each change takes ten seconds: load the home page, load the login page, log in to the admin dashboard, upload a media file, and submit a form if the site has one. If the site breaks, you know exactly which change broke it, because it is the one you just made. If you paste all twenty rules at once and the site breaks, you have twenty suspects and no way to tell which one is guilty except by removing them one at a time. The testing checklist at the end of this guide lists the five checks in order, so you can run the same test after every change without thinking about it.
One more decision to make before you start: know which web server your site runs on. The .htaccess half of this guide only works on Apache and on LiteSpeed, which reads .htaccess files the same way Apache does. On nginx, .htaccess is ignored entirely, and the equivalent rules go in the server block, usually through the host’s panel or support. If you are not sure which server you run, ask your host, or check the response headers of any page on your site. The wp-config.php half works everywhere, because it is PHP, not server configuration. The .htaccess half is Apache and LiteSpeed only, and every section in that half says so where it matters.
What Lives in wp-config.php
Before hardening a file, it helps to know what is actually in it, because the rules that follow are all consequences of the contents. A standard wp-config.php is short, usually under a hundred lines, and it holds four things: the database connection details, the eight secret keys and salts, the table prefix, and a handful of constants. The file is generated when WordPress is installed, and most site owners never open it again, which is exactly why it is worth opening now.
The database details are four lines near the top:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
These four lines are the keys to the kingdom. Anyone who can read them can connect to the database directly, read every post and user, change any setting, and create a new administrator account for themselves. The password should be long, random, and unique to this site, and the database user should only have access to this site’s database, not to any other database on the server. If your database password is a word you chose yourself, or the same password you use anywhere else, change it now, through your hosting panel, and then update this line to match. The file itself is also the reason the database password should never appear in a screenshot, a tutorial, or a support ticket. Every time the file is shared, the password is shared with it.
The eight secret keys and salts come next. They look like long random strings, and they are:
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );
On a real site the placeholders are replaced by long random strings. These eight lines sign every login cookie WordPress issues. When you log in, WordPress uses these keys to create a cookie that proves you are who you say you are, and it checks the same keys every time you load a page. If the keys are weak, copied from a tutorial, or leaked in a backup, an attacker can forge a login cookie and sign in as any user, including an administrator, without knowing any password. That is why replacing them is one of the highest-value steps in this guide, and why the section on salts comes before the section on constants.
The table prefix is a single line, usually wp_:
$table_prefix = 'wp_';
The prefix is added to the start of every table name in the database, so the posts table is wp_posts, the users table is wp_users, and so on. The default wp_ prefix makes automated SQL injection attempts slightly easier, because the attacker already knows the table names. On a new installation, choosing a different prefix costs nothing. On an existing site, changing it means renaming every table and updating every reference, which is a job for a plugin or a careful manual migration, not a quick edit. The honest advice for an existing site is to leave the prefix alone and spend the effort on the rules that matter more. This guide does not recommend changing the prefix on a live site.
The rest of the file is constants, and the section on constants below covers the ones worth setting. Everything else in the file is either generated by the installer or specific to your host, and should be left alone unless you know what it does. The WordPress developer handbook page on editing wp-config.php calls it one of the most important files in a WordPress install, and warns that some changes can cause unforeseen issues, which is why you should know how to restore the file before you modify any setting. You now have a backup, so you do.
File Permissions: 400 for wp-config.php, 644 for Everything Else
File permissions decide who can read a file and who can write to it, and they are the cheapest protection in this whole guide, because they cost nothing and they work even if every other rule fails. The WordPress hardening guide gives 755 for directories and 644 for files as the general scheme, with wp-config.php tightened beyond that. The numbers are Unix permission modes, and they mean the following: 755 lets the owner read, write, and execute, and everyone else read and execute; 644 lets the owner read and write, and everyone else read only; 400 lets only the owner read, and nobody write; 440 lets the owner and the group read, and nobody write.
The scheme for a WordPress site is simple. Directories are 755. Ordinary files are 644. wp-config.php is 400 or 440, so only you and the web server can read it, and nobody can write to it through the file system. On most hosts where PHP runs as your user, 600, owner read and write only, or 640 also works, and some guides recommend those instead. The official hardening guide states the tighter end of the range, 400 or 440, and that is what this guide recommends, with one practical note: if your host runs PHP as a different user from your file owner, 400 can stop WordPress from reading its own configuration file, and the site will white-screen. If that happens, step up to 440, then 640, until the site loads. The right permission is the tightest one that still lets the site work, and the only way to find it is to test.
Setting permissions depends on how you reach the server. In an FTP client such as FileZilla, right-click the file, choose file permissions, and type the number. In a hosting file manager, select the file, open permissions, and set the same number. Over SSH, the command is:
chmod 400 wp-config.php
To check what the permissions currently are, run:
ls -l wp-config.php
The output shows a string such as -r--------, which is 400, or -rw-r--r--, which is 644. If your wp-config.php is currently 644, which is the default on many hosts, tightening it to 400 is the single fastest improvement in this guide, and it takes ten seconds. While you are there, check the permissions of the directories too. A directory set to 777, readable and writable by everyone, is a common leftover from a troubleshooting session, and it should never stay that way on a live site. Directories are 755, files are 644, wp-config.php is 400 or 440. Memorise the three numbers and the whole scheme fits in one sentence.
Fresh Salts and Keys: Rotate Them Now, and After Every Incident
The eight keys and salts are the most powerful lines in wp-config.php, and they are also the easiest to fix, because WordPress.org generates fresh ones for you. Visit the official secret-key service at api.wordpress.org/secret-key/1.1/salt/, and it returns eight fresh lines each time you open it. Replace the whole block in wp-config.php with the new lines, keeping the define() format exactly as shown, and save. That is the whole job by hand.
If you have WP-CLI on the server, the same job is one command, and it rewrites the values in place:
wp config shuffle-salts
Either way, the effect is the same, and it is worth understanding before you do it, because it looks alarming the first time. Replacing the salts logs every user out, including you. Every login cookie on the site was signed with the old keys, and the new keys cannot verify them, so every session ends immediately. That is exactly what you want after a suspected break-in, because it also ends the attacker’s session, and it is exactly what you want after removing a former administrator or developer, because it ends their access too. Log back in afterwards and carry on. Nothing else changes: posts, settings, and users are all untouched.
Rotate the salts in three situations. First, now, if you have never done it, or if the keys in your file are the placeholders from a tutorial, or the same keys you have used on more than one site. Keys copied from a tutorial are public by definition, and keys shared between sites mean a breach on one site compromises the other. Second, after any security incident, including a suspected one. If you are not sure whether the site was breached, rotate them anyway; the cost is one extra login, and the benefit is that any forged cookie the attacker created stops working. Third, whenever someone with administrator access leaves, whether that is a developer, a contractor, or a staff member. Access that is not revoked is access that is still live.
There is one mistake to avoid. Do not generate the salts once and paste them into several sites. Each site needs its own set, generated separately. The generator returns a fresh set on every visit, so open it once per site and paste what it gives you. A shared set of keys across sites is barely better than the tutorial placeholder, because a leak anywhere becomes a leak everywhere.
Move wp-config.php Above the Web Root
WordPress looks for wp-config.php one directory above the site root if it is not in the root, which gives you a simple way to put the file outside the folder the web server can serve. On most hosts, the site lives in a folder such as public_html, and the folder above it, your home directory, is not web-accessible at all. Moving the file there means that even if the server misconfigures PHP and starts serving PHP files as plain text, there is no wp-config.php in the web folder to serve.
The move itself is simple: download the file, upload it one level up, and delete the original from the web root. WordPress finds it automatically, with no configuration change. There are two things to check before you do it. First, confirm that your host allows it. Some managed hosts, and some hosts with custom control panels, expect the file in the root and will regenerate it there, or break their own tooling if it moves. If your host’s documentation says not to move it, do not move it; the permissions and .htaccess rules in this guide achieve most of the same protection without the move. Second, confirm that the folder above the web root is actually outside the web root. On some hosts the folder above public_html is still served under a different path. If you are not sure, ask the host.
If you cannot move the file, do not worry. Correct file permissions, 400 or 440, achieve most of the same protection, and the .htaccess rule in the next half of this guide blocks direct web access to the file as a second layer. Moving the file is the strongest option, but it is not the only option, and a file at 400 with a deny rule is already well protected.
Delete Stray Copies: .bak, .old, .orig, and the Editor Backup
A backup copy of wp-config.php is the file, as far as an attacker is concerned. The original may be locked down to 400 and hidden behind a deny rule, but a copy named wp-config.php.bak in the same folder is an ordinary file with ordinary permissions, and if the server serves it as text, the database password is on the screen. This is one of the most common ways wp-config.php contents leak, and it happens because the copies are made by routine, invisible actions: a text editor that saves a backup on every edit, a migration plugin that copies the file before changing it, a developer who duplicates it “just in case” and forgets it.
Search your site for copies now. Over SSH, from the site root:
find . -name 'wp-config*' -type f
In a file manager, search for wp-config and look at every result. Delete every copy that is not the live file, including files ending in .bak, .old, .orig, .save, .swp, .swo, and .sql, and any file with a tilde at the end, which is how some editors mark a backup. Keep your real backup, the one you made at the start of this guide, on your own computer, not on the server. The same rule applies to any other sensitive file: a database dump left in the web root is a copy of the whole database, and a .env file or a .git folder left in the web root leaks credentials and source code. If you find any of those, remove them from the server or move them outside the web root, and then block them in .htaccess as well, which the sensitive-files rule below does.
One more habit to set while you are here. Configure your text editor not to create backup copies on the server, or to store them in a folder on your own computer instead. The editor backup that saves you from a bad edit is the same file that leaks your database password if it stays on the server. A backup that lives on your computer is safe. A backup that lives in the web root is a vulnerability with a helpful name.
The Seven Constants Worth Setting in wp-config.php
Constants are the settings you add to wp-config.php to change how WordPress behaves, and they go above the line that reads “That is all, stop editing”, because everything below that line is WordPress loading, not configuration. Together with the permissions and the salts above, these seven lines are the core of WordPress wp-config hardening on the configuration side. The full set worth setting fits in one block, and you can paste it as a whole, after reading what each line does. Every line is documented in the WordPress developer handbook page on editing wp-config.php.
define( 'WP_ENVIRONMENT_TYPE', 'production' );
define( 'DISALLOW_FILE_EDIT', true );
define( 'DISALLOW_UNFILTERED_HTML', true );
define( 'FORCE_SSL_ADMIN', true );
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
The first line, WP_ENVIRONMENT_TYPE, tells WordPress and its plugins that this is a production site, not a development or staging copy. Some plugins change their behaviour based on it, for example by disabling debug output or test modes, and setting it explicitly removes any guesswork. It costs nothing and it documents the intent of the site in the file itself.
DISALLOW_FILE_EDIT is the single most important constant in this guide. It removes the theme and plugin code editors from the dashboard entirely. Without it, anyone with an administrator login can open the editor, paste PHP into functions.php, and run any code they like on the server. With it set, that path is closed. The practical effect is that a stolen admin session, a phished password, or a compromised administrator account can no longer turn into remote code execution in about ten seconds from the browser. The constant does not stop file uploads, and it does not stop an attacker who already has file-system access, so it is one layer, not a fix, but it is the layer that closes the easiest path from a stolen login to a full takeover. Set it on every site you run. If you genuinely edit theme files from the dashboard, which you should not, stop, edit them in a code editor and upload them instead, and set the constant.
There is a stronger sibling, DISALLOW_FILE_MODS, which also disables plugin and theme installation and updates from the dashboard. It is stronger, but it also blocks one-click updates, so only use it on sites where updates run through WP-CLI or a deployment pipeline. On a normal site, where the owner updates plugins from the dashboard, it would break the update workflow, and the owner would turn it off again within a week. Leave it commented out, as in the block below, unless your workflow supports it:
// Only if you update exclusively through WP-CLI or a deployment tool:
// define( 'DISALLOW_FILE_MODS', true );
DISALLOW_UNFILTERED_HTML stops even administrators and editors from saving raw script tags in content. On a multi-author site, where not every author is fully trusted, this closes a stored cross-site scripting path that the Day 25 guide covers in detail. On a single-author site it changes little, but it costs nothing, so set it anyway.
FORCE_SSL_ADMIN forces the admin and login screens to use HTTPS, even if a link or a redirect points to the plain HTTP version. If your site already redirects all traffic to HTTPS, which it should, this constant is a second layer that protects the login specifically. It does nothing on a site without a valid SSL certificate, so install the certificate first, confirm the site loads over HTTPS, and then set the constant.
WP_AUTO_UPDATE_CORE set to minor means WordPress installs minor security releases automatically, and asks before major version upgrades. This is the default behaviour on most sites, and the constant is worth setting explicitly because it documents the intent, and because it guards against a plugin or a host setting that quietly disabled automatic updates. While you are in the file, check that nothing in it disables updates outright. A line such as define( 'AUTOMATIC_UPDATER_DISABLED', true ); means security patches are not being installed, and it should be removed.
The last three lines are the debug settings, and they belong together. WP_DEBUG false turns WordPress debug mode off. WP_DEBUG_LOG true writes any errors that do occur to a log file instead of the screen. WP_DEBUG_DISPLAY false makes sure errors are never shown to visitors. On a live site, debug output is a leak: PHP error messages reveal file paths, function names, database table names, and server details that help an attacker map the site. On a development site, debug output is useful, which is why the constant exists at all, but a live site should never show it. If you need to debug a live problem, turn the log on, reproduce the problem, read the log file, and turn it off again. The log file itself must not be publicly accessible, so keep it outside the web root, or block it in .htaccess, as the sensitive-files rule below does. The Day 26 guide on PHP sessions covers the same debug discipline for session errors, and the rule is the same there: log privately, never display publicly.
There are two more constants that belong on some sites, and the decision depends on the site. WP_HTTP_BLOCK_EXTERNAL makes the WordPress HTTP API refuse every external host except your own site, with WP_ACCESSIBLE_HOSTS taking a comma-separated allowlist, for example api.wordpress.org and the update servers your plugins need. This is powerful on a site that never needs to call an outside service, and it breaks any plugin that fetches data from an outside API, so test it carefully and only use it if you know which hosts your plugins need. The second is a constant you should not set without thinking: WP_POST_REVISIONS and EMPTY_TRASH_DAYS are performance and housekeeping settings, not security settings, and they do not belong in a hardening guide. Keep the block to the seven lines above, plus the commented DISALLOW_FILE_MODS, and the file stays readable.
Database Credentials: Unique User, Strong Password, Least Privilege
The database password in wp-config.php deserves its own short section, because it is the one secret in the file that many site owners set once at install and never think about again. Three rules apply, and all three are cheap.
First, the password must be long, random, and unique to this site. If it is a word you chose, a date, or a password you use anywhere else, change it. Generate a new one in your hosting panel, usually under the database or MySQL section, and then update the DB_PASSWORD line in wp-config.php to match. Change the password in the panel first and the file second, and keep the site loading in between by doing the two edits back to back. A site with a mismatched password shows a database connection error, which is easy to fix by correcting the line, but alarming if you are not expecting it.
Second, the database user should only have access to this site’s database. At install time, many hosts create a user with access to every database on the account, which means a breach on one site gives the attacker the credentials to read another site’s database. Check the user’s privileges in the hosting panel and restrict them to the one database. This is the database version of the least-privilege principle, and it limits the damage if the password ever leaks.
Third, keep the secret out of the places where it does not belong. Do not paste wp-config.php into a support ticket, a forum post, a screenshot, or a chat message. Do not commit it to a public code repository. If you use version control, add it to the ignore file and deploy it separately. On advanced setups, the password can be passed through the PHP-FPM pool configuration instead of living in the file at all, with the pool file holding the value and wp-config.php reading it from the environment, but that is a server-administration job, and the three rules above are enough for most sites.
The Table Prefix: Change It on New Sites Only
The default table prefix is wp_, and on a new installation you should choose something else, because the default makes automated SQL injection attempts slightly easier: the attacker already knows the table names. Most installers let you set the prefix during setup, and a random string of a few letters and an underscore is enough. It does not need to be secret, and it does not need to be long. It just needs to not be wp_.
On an existing site, leave the prefix alone. Changing it means renaming every table in the database and updating every reference to the old names, including the options table and the user metadata, where the prefix appears inside stored values. Plugins exist that do the rename, and WP-CLI can do it with care, but the risk of breaking the site outweighs the benefit, which is small. The prefix is security through obscurity, and obscurity is the weakest layer there is. Spend the effort on the rules in this guide that actually reduce risk, and treat the prefix as a decision for the next site you build, not this one.
The Complete Hardened wp-config.php
Here is the full set of changes to wp-config.php, assembled as one block you can work through in order. The lines are shown as they should appear in the file, with comments explaining each group. Add them above the line that reads “That is all, stop editing”. Do not paste the database lines from this example over your own, because the values are placeholders: your database name, user, and password stay exactly as they are, and your salts come from the generator.
/* Database settings — keep your own values, change only the password if it is weak */
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_strong_unique_password' );
define( 'DB_HOST', 'localhost' );
/* Fresh salts — generate at api.wordpress.org/secret-key/1.1/salt/ */
define( 'AUTH_KEY', 'paste your fresh value here' );
define( 'SECURE_AUTH_KEY', 'paste your fresh value here' );
define( 'LOGGED_IN_KEY', 'paste your fresh value here' );
define( 'NONCE_KEY', 'paste your fresh value here' );
define( 'AUTH_SALT', 'paste your fresh value here' );
define( 'SECURE_AUTH_SALT', 'paste your fresh value here' );
define( 'LOGGED_IN_SALT', 'paste your fresh value here' );
define( 'NONCE_SALT', 'paste your fresh value here' );
/* Hardening constants */
define( 'WP_ENVIRONMENT_TYPE', 'production' );
define( 'DISALLOW_FILE_EDIT', true );
define( 'DISALLOW_UNFILTERED_HTML', true );
define( 'FORCE_SSL_ADMIN', true );
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
/* That's all, stop editing! Happy publishing. */
After editing, set the file permissions to 400 or 440, load the site, and confirm the home page, the login page, and the admin dashboard all work. Then confirm the dashboard code editors are gone: in the admin menu, the Theme File Editor and Plugin File Editor should no longer appear. If they still appear, the constant is below the “stop editing” line, or in the wrong file, or cached by a plugin. Move it above the line and try again.
What .htaccess Is, and Where It Works
The second file in this guide is .htaccess, and it is a different kind of file from wp-config.php. Where wp-config.php is PHP that WordPress reads once at startup, .htaccess is a server configuration file that Apache reads on every request, before WordPress loads a single line of code. It is a plain text file that lives in the site root, and it holds directives: instructions to the server about how to handle requests for files in that folder and every folder below it. WordPress itself writes to it, which is why the file already contains a block marked # BEGIN WordPress and # END WordPress, holding the rewrite rules that make pretty permalinks work.
The first thing to settle is where .htaccess works at all. It is an Apache feature, and LiteSpeed reads it too, which covers the two most common WordPress hosts. On nginx it is ignored entirely, and the same rules go in the server block configuration instead, usually through the host’s panel or support. If your site runs on nginx, the wp-config.php half of this guide still applies in full, and the .htaccess half needs translating into nginx syntax, which your host can usually do from this guide. This site runs on LiteSpeed, so .htaccess rules apply here, and the examples below are written for Apache 2.4 syntax, which LiteSpeed also understands.
The second thing to settle is where your rules go. Your own rules go outside the # BEGIN WordPress block, either above it or below it. WordPress rewrites everything between those two markers whenever you change the permalink settings, save a plugin setting, or update, and any rule you placed inside the block is deleted without warning. Place your rules above the block, with a comment marking them as yours, and they survive every WordPress update. If you ever change the permalink structure and the site breaks, check the block first: WordPress may have rewritten it, but your rules above it will still be there.
The third thing to settle is the cost of a mistake. A wrong directive in .htaccess gives a 500 error on every page, because Apache cannot parse the file and refuses to serve anything. The fix is to remove the line you just added, which is why you keep a backup and change one rule at a time. Some hosts also restrict which directives may appear in .htaccess at all. If your host does not allow the Options directive, for example, adding it will produce a server error, and the fix is to remove the line and ask the host, or set it in the virtual host configuration instead. The testing checklist at the end of this guide tells you exactly what to check after each change, so a mistake is caught in ten seconds, not discovered by a visitor an hour later.
Rule 1: Turn Off Directory Listing
If a folder has no index file, Apache may list its contents to anyone who requests the folder. That listing is a free inventory of the site for an attacker: backup files, SQL dumps, old plugin folders, stray uploads, and anything else that was left in a folder and forgotten. The fix is one line, placed at the top of the root .htaccess file:
Options -Indexes
With that line in place, requesting a folder without an index file returns a 403 error instead of a listing. The rule costs nothing and breaks nothing, because a well-run site never relies on directory listings. Set it first, before the other rules, and keep it there. If your host does not allow the Options directive in .htaccess, the site will return a 500 error when you add the line. Remove it, and ask the host to set it in the virtual host configuration, where it belongs on that setup.
Rule 2: Deny Direct Access to wp-config.php
The file holds the database credentials and the secret keys, and it should never be reachable over the web. While PHP is running, a direct request to it returns a blank page, because PHP executes the file instead of printing it, which is why the risk feels theoretical. But the rule exists for the failure case: the day PHP stops running, the day a server update misconfigures the handler, the day a migration moves the site to a server that serves PHP files as text. On that day, a request for wp-config.php returns the database password in plain text, unless this rule is in place. Add it to the root .htaccess file:
<Files wp-config.php>
Require all denied
</Files>
This uses Apache 2.4 syntax. On Apache 2.2 the older Order allow,deny and Deny from all syntax applies, and some older tutorials still show it. The old syntax still runs on Apache 2.4 through a compatibility module, but it is deprecated, and new rules should use Require all denied. If your host runs Apache 2.2, which is unlikely in 2026 but not impossible on an old server, use the older form, and plan the server upgrade, because Apache 2.2 has been out of support for years.
Test the rule by requesting the file in a browser: visit yoursite.com/wp-config.php and confirm you receive a 403 error, not a blank page and not the file contents. A blank page means PHP executed the file, which is the normal safe behaviour, but the rule should still be in place for the failure case. A 403 means the rule is working. If you see the file contents, stop, fix the rule before doing anything else, and check whether a copy of the file exists anywhere else on the server.
Rule 3: Protect the Other Sensitive Files
wp-config.php is the most sensitive file, but it is not the only one. The same deny rule should cover every file that was never meant to be requested by a visitor: the .htaccess file itself, the password file .htpasswd, the readme and license files that reveal the WordPress version, the debug log, and any backup or archive file left in the web root. One rule covers them all:
<FilesMatch "^(\.htaccess|\.htpasswd|wp-config\.php|readme\.html|license\.txt|xmlrpc\.php)$">
Require all denied
</FilesMatch>
Think before including xmlrpc.php in that list, because blocking it breaks some apps, Jetpack features, and older publishing tools. The xmlrpc decision has its own section below, and the rule here shows it as an option, not a default. If you are unsure, leave xmlrpc.php out of this rule and handle it separately.
The same thinking applies to hidden files and folders that should never be in the web root at all. A .git folder leaks the source code history, including old credentials. A .env file leaks environment secrets. A backup archive, a SQL dump, or a zip of the site left in the web root is a download link to the whole site. The correct fix is to remove them from the server, or move them outside the web root, and the .htaccess rule is the second layer for the day you forget. Add a rule that denies access to hidden files and common backup extensions:
<FilesMatch "^\.|(\.bak|\.old|\.orig|\.save|\.swp|\.sql|\.log)$">
Require all denied
</FilesMatch>
This blocks any file starting with a dot and any file ending in the listed backup extensions. It is a safety net, not a substitute for cleaning up. The files should not be on the server in the first place, and the rule exists so that the one you missed does not become the breach.
Rule 4: No PHP Execution in Uploads
This is the rule that matters most, and it is the one the Day 27 guide on file upload security builds on. Attackers who manage to upload a script into wp-content/uploads then run it by visiting its URL. The upload filter stops most malicious files from being uploaded, but filters have bypasses, and a filter that blocks .php may still allow .phtml or .phar, which the server may also execute as PHP. The .htaccess rule closes the gap at the server level: even if a script reaches the uploads folder, requesting it returns a 403 instead of executing it.
Create a file named .htaccess inside wp-content/uploads, with this content:
<FilesMatch "\.(php|phtml|php[0-9]|phar)$">
Require all denied
</FilesMatch>
The extension list matters, and it is worth getting right. A rule that blocks only .php leaves .phtml and .phar open, and both can execute as PHP on a standard server. A test on Apache 2.4.58 with PHP 8.3.6 in November 2026 showed exactly that: with a rule matching only .php, a test.php request returned 403, but test.phtml and test.phar both returned 200 and printed their output, which means the PHP ran. The rule above blocks all of them, including numbered variants such as php5 or php8, which some servers also execute. If a plugin stores PHP in the uploads folder on purpose, which is unusual, test it first, because this rule will stop that plugin from working, and the plugin will need a different storage location.
Note that this rule goes in a separate .htaccess file inside the uploads folder, not in the root file. The root file governs the whole site, and a rule that denied PHP execution everywhere would stop WordPress itself from running. The uploads file governs only that folder and the folders below it, which is exactly the scope you want. The Day 27 guide covers the upload filter and the server-level block in more detail, including the nginx equivalent for sites that do not use .htaccess at all.
Rule 5: Block xmlrpc.php, Only If Nothing Uses It
The xmlrpc.php endpoint was built for remote publishing and trackbacks, back when desktop blogging clients and the WordPress mobile apps needed it. Today it is primarily a target: brute-force attacks try thousands of passwords against it, because a single request to it can test hundreds of username and password combinations at once, and amplification attacks use it to bounce traffic at other sites. WordPress’s own brute-force guidance calls it a frequent target. The block is one rule in the root .htaccess file:
<Files xmlrpc.php>
Require all denied
</Files>
The decision is whether to block it, and the answer depends on what your site uses. Blocking it breaks Jetpack, because Jetpack communicates with your site through XML-RPC, and it breaks the WordPress mobile apps and some older publishing tools. If you use Jetpack, or you publish from the mobile app, do not block it, or block it and accept that those tools stop working. If you publish only from the dashboard in a browser, and you do not use Jetpack, block it, because nothing on your site needs it and the endpoint only attracts attacks.
There is a middle option if you want the protection without the breakage. Some security plugins offer a selective XML-RPC block that allows Jetpack’s servers and blocks everyone else, and some hosts offer the same at the server level. That is a reasonable compromise on a site that needs Jetpack. On a site that does not need it, the full block is simpler and stronger, and the rule above is all it takes. Whatever you decide, decide it deliberately, test the tools you use after adding the rule, and do not paste the block blindly because a checklist told you to.
Rule 6: Block Direct Access to wp-includes Scripts
WordPress ships a folder named wp-includes that holds the core PHP files, and most of those files are only meant to be included by other PHP files, never requested directly by a visitor. WordPress itself provides a block for this in the default .htaccess rules on some installs, and it is worth adding explicitly if your install does not have it:
# Block direct access to include-only scripts
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
This is the block WordPress has shipped in its own hardening documentation, and it refuses direct requests to scripts that are only meant to be included, while allowing the files that legitimately need direct access, such as the JavaScript and CSS in the includes folder. The [F] flag returns a 403, and the [L] flag stops processing further rules. Do not edit this block by hand unless you know what each line does. Paste it as shown, outside the # BEGIN WordPress block, and test the site thoroughly afterwards, because a mistake here can break the admin dashboard or the media library in ways that are not obvious from the home page.
Rule 7: Restrict wp-login.php by IP, Only If Your Address Never Changes
The login page is the most attacked page on any WordPress site, because every bot on the internet knows where it is. Restricting it to your own IP address is the strongest single .htaccess rule, and also the one most likely to lock you out, so it comes with the clearest warning in this guide: only use it if your administrators log in from a fixed address that never changes.
<Files wp-login.php>
Require ip 203.0.113.10
</Files>
Replace the address with your own public IP address, which you can find by searching for “what is my IP” from the network you log in from. If you log in from home, from the office, and from a phone on mobile data, you have three addresses, and the rule needs all three, or you will be locked out from the other two. If your internet provider gives you a new address every time the router restarts, which most home providers do, the rule will lock you out the next time the address changes, and the fix is to edit the file over FTP or through the hosting panel, which is exactly the kind of emergency edit this guide is trying to avoid.
For most site owners, the honest answer is not to use this rule, and to rely on the other protections instead: strong passwords, two-factor authentication, a limit on login attempts, and the xmlrpc block, which stops the most common brute-force path. If you run a site with one administrator, one office, and one fixed IP address, the rule is worth it, and it turns the login page from a public target into a private one. If that does not describe your site, skip the rule and say so in your notes, so a future hardening pass does not add it blindly.
Security Headers in .htaccess: Free Protection, Carefully Applied
The response headers that tell browsers how to behave also live in .htaccess, or in the server configuration, and they are worth adding, because they cost nothing and they break nothing when set conservatively. They need the mod_headers module, which most Apache and LiteSpeed hosts enable by default, so wrap them in a check that only applies them when the module is present:
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>
Each header does one job. X-Content-Type-Options with nosniff stops the browser from guessing the content type of a file, which prevents an uploaded file from being treated as executable code when it was served as something else. X-Frame-Options with SAMEORIGIN stops other sites from loading your pages inside a frame, which prevents clickjacking, where an attacker tricks a visitor into clicking something on your site while thinking they are clicking something else. Referrer-Policy limits how much of the page address is sent to other sites when a visitor follows a link, which is a small privacy win. Permissions-Policy disables browser features the site does not use, such as geolocation, the microphone, and the camera, so a cross-site scripting flaw cannot turn them on.
Two headers need care, and they are deliberately left out of the block above. Strict-Transport-Security, the HSTS header, tells browsers to use HTTPS for your site for a set period, and once a browser has seen it, there is no way to undo it until the period expires. If your HTTPS setup is not fully working, including every subdomain, setting HSTS can lock visitors out of the plain HTTP version permanently. Set it only after confirming the whole site, including subdomains, works over HTTPS, and start with a short period such as one day before raising it. The second is Content-Security-Policy, which restricts where the browser may load scripts, styles, and images from. It is powerful, and it is also the header most likely to break a site, because a WordPress site loads resources from many places: the theme, the plugins, the CDN, the analytics service, the font provider, and the embed provider. A strict policy will block some of them, and the site will look broken in ways that are hard to diagnose. If you want a content security policy, build it on a staging site, watch the browser console for blocked resources, and add each source deliberately. Do not paste a policy from a tutorial into a live site and hope for the best.
The Complete Hardened .htaccess
Here is the full set of root .htaccess rules from this guide, assembled as one block. Place it above the # BEGIN WordPress block, and keep the uploads rule in its own file inside wp-content/uploads, as the section above explains. The xmlrpc rule is included as a commented option, so you can enable it deliberately after checking what your site uses. The IP restriction on the login page is left out entirely, because it locks out most site owners, and the section above explains when to add it.
# BEGIN All About HTML hardening — keep above the WordPress block
Options -Indexes
# Protect wp-config.php and other sensitive files
<FilesMatch "^(\.htaccess|\.htpasswd|wp-config\.php|readme\.html|license\.txt)$">
Require all denied
</FilesMatch>
# Block backup copies and hidden files
<FilesMatch "^\.|(\.bak|\.old|\.orig|\.save|\.swp|\.sql|\.log)$">
Require all denied
</FilesMatch>
# Block xmlrpc.php — enable only if nothing on your site uses it
# <Files xmlrpc.php>
# Require all denied
# </Files>
# Block direct access to wp-includes scripts
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
# Security headers
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
</IfModule>
# END All About HTML hardening
# BEGIN WordPress
# (your existing WordPress rules stay here, unchanged)
# END WordPress
After adding the block, test the site with the checklist below. If the site returns a 500 error, remove the block, restore your backup copy of the file, and add the rules one at a time to find the one your host rejects. The most common rejection is the Options -Indexes line on a host that does not allow it in .htaccess. The second most common is a host that runs an older Apache version and rejects the 2.4 syntax. Both are fixed by moving the rule to the server configuration, which your host can do.
What Not to Copy: The Padding in Most Hardening Guides
Most hardening guides list thirty settings, and about eight of them matter. The rest is padding, and it is worth knowing which is which, so you do not waste an afternoon on rules that change nothing, and so you do not break your site with a rule that was never needed.
Hiding your WordPress version is marginal. The version is visible in the page source, in the readme file, in the RSS feed, and in a dozen other places, and an attacker who wants to know it will find it. Removing it from one place while leaving it in the others changes nothing. The readme rule above blocks the file, which is enough. Do not spend time stripping version numbers from every output.
A 200-line bad-bot user-agent blocklist is pure theatre. Bots change their user agent on every request, and a blocklist that names yesterday’s bots does nothing about tomorrow’s. The login protections in this guide, the xmlrpc block, and a limit on login attempts do more than any blocklist, because they protect the endpoint instead of trying to recognise the attacker.
Author redirects, which send requests for author archive pages to the home page, are marginal too. They hide usernames, which is a small win, but usernames are also visible in the REST API, in RSS feeds, and in the author links on every post. Strong passwords and two-factor authentication protect the account regardless of whether the username is known. If you use a security plugin that offers the redirect, leave it on. If you do not, do not add it by hand for this reason alone.
Server version hiding, through ServerTokens Prod and ServerSignature Off, is real protection, but it belongs in the main Apache configuration, not in .htaccess, because .htaccess cannot set it on most hosts. If you control the server, set it there. If you are on shared hosting, ask the host. Do not paste it into .htaccess and assume it worked, because on most shared hosts it silently does nothing.
The test for any rule you are considering is simple: does it reduce what an attacker can reach, or what they can do with it? If the answer is yes, and the cost is low, keep it. If the answer is no, or the cost is a broken plugin, a locked-out administrator, or an afternoon of debugging, drop it. The rules in this guide all pass that test. The padding fails it.
File Permissions Reference
The permission scheme for the whole site fits in one table. Use it when you set permissions, and use it again when you audit them.
| Path | Permission | Who can read it | Who can write to it |
|---|---|---|---|
| Directories | 755 | Owner, group, everyone | Owner only |
| Ordinary files | 644 | Owner, group, everyone | Owner only |
wp-config.php | 400 or 440 | Owner only, or owner and group | Nobody through the file system |
.htaccess | 644 | Owner, group, everyone | Owner only |
wp-content/uploads | 755 | Owner, group, everyone | Owner only |
wp-content/uploads/.htaccess | 644 | Owner, group, everyone | Owner only |
| Debug log, if inside the web root | 600 | Owner only | Owner only |
| Any directory | Never 777 | Everyone | Everyone, including attackers |
The last row is the one that matters most. A directory set to 777 is readable, writable, and executable by everyone, including any other account on a shared server and any script that finds its way onto it. It is a common leftover from a troubleshooting session where someone loosened the permissions to make something work and never tightened them again. Search for 777 directories now, and set them back to 755:
find . -type d -perm 777
If that command returns any results, fix each one before you do anything else in this guide.
Test After Every Change: The Five Checks
Testing is not a step you do at the end. It is a step you do after every change, and it takes ten seconds. Run these five checks in order, after each rule you add, and a mistake is caught while you still know which change caused it.
- Load the home page. It should load normally, with no error, no missing styles, and no missing images. A 500 error here means the last
.htaccesschange is invalid. A white screen means the lastwp-config.phpchange has a syntax error. Remove the change, restore the backup copy, and try again. - Load the login page. It should show the login form. A 403 error here means a rule is blocking it, most likely the IP restriction or a FilesMatch rule that is too broad. A redirect loop means the SSL constant is fighting with the host’s own redirect, and the constant should be removed while you check the host’s settings.
- Log in to the admin dashboard. It should load fully, with the menu, the widgets, and the editor. A broken dashboard after adding the wp-includes block means the block is too strict for your install, and it should be removed while you check the version. Confirm the Theme File Editor and Plugin File Editor are gone from the menu, which proves
DISALLOW_FILE_EDITis working. - Upload a media file. Go to the media library, add a new image, and confirm it uploads and displays. A failure here means the uploads
.htaccessrule is too broad, or the permissions on the uploads folder are wrong. The rule should block PHP files, not images, so an image upload that fails points to the permissions, not the rule. - Submit a form, if the site has one. A contact form, a comment, or a search, any action that writes to the database. A failure here means a constant or a rule is interfering with the request, most likely the HTTP blocking constant if you set it, or a header that is too strict. Remove the last change and test again.
If all five checks pass, move on to the next rule. If any check fails, stop, remove the last change, and do not add the next rule until the site is working again. A site that is half-hardened and working is safer than a site that is fully hardened and broken, because a broken site gets its hardening removed in a panic, and the panic removal usually removes all of it.
Seven Mistakes That Undo All of This Work
Hardening fails in predictable ways, and the mistakes below account for most of them. They are worth reading as a list, because each one is easy to make and easy to miss.
| Mistake | What happens | How to avoid it |
|---|---|---|
| Editing without a backup | A typo white-screens the site and there is no copy to restore | Download both files before you start, and keep the copies off the server |
| Pasting all rules at once | The site breaks and you have twenty suspects | Change one rule at a time and test after each one |
| Putting rules inside the WordPress block | WordPress overwrites them on the next save | Place your rules above # BEGIN WordPress, with a comment marking them |
| Leaving backup copies on the server | The copy leaks the database password as plain text | Delete every .bak, .old, and .orig copy, and keep your backup on your computer |
Blocking only .php in uploads | .phtml and .phar still execute as PHP | Use the full extension list: php, phtml, php[0-9], phar |
| Blocking xmlrpc without checking | Jetpack and the mobile apps stop working | Check what your site uses before you block it, and test afterwards |
| Locking the login page to a changing IP | Your address changes and you are locked out | Only restrict by IP on a fixed address, and keep FTP access ready |
| Forgetting the debug log | The log file sits in the web root and leaks errors | Keep the log outside the web root, or block it in .htaccess |
The last row deserves emphasis, because it is the mistake that hides inside a correct-looking setup. WP_DEBUG_LOG writes errors to a file, and on many installs that file lands in wp-content with a predictable name, where anyone can request it and read the errors. The errors include file paths, database queries, and plugin names, which is exactly the information the debug settings were meant to hide. If you use the log, give it a path outside the web root, as the constants section shows, or block the log file in .htaccess with the backup-extensions rule, which already covers .log files. A private log is a diagnostic tool. A public log is a leak.
Frequently Asked Questions
1. What is wp-config.php and why does it matter for security?
wp-config.php is the file WordPress reads before anything else loads, and it holds the database name, username, and password, the eight secret keys and salts that sign every login cookie, the table prefix, and the constants that change how WordPress behaves. It matters because anyone who can read it can connect to the database directly and change any setting, create an administrator account, or read every post and user without logging in. It is not normally served to visitors, because PHP executes it instead of printing it, but a server misconfiguration, a failed PHP upgrade, or a backup copy left in the web root can expose it as plain text. The whole wp-config.php half of this guide exists to protect the file against that failure case, and against the quieter risk that it is readable by more accounts on the server than it needs to be.
2. What permissions should wp-config.php have?
Set wp-config.php to 400 or 440, so only you and the web server can read it, and nobody can write to it through the file system. Directories on the site are 755 and ordinary files are 644, and wp-config.php is tightened beyond that because it holds the database password. On most hosts where PHP runs as your user, 600 or 640 also works. If the site white-screens after tightening to 400, step up to 440, then 640, until the site loads, because some hosts run PHP as a different user and need the group read permission. The right permission is the tightest one that still lets the site work. While you are there, check that no directory is set to 777, which lets everyone read and write it, and set any you find back to 755.
3. How do I generate fresh WordPress salts and keys?
Visit the official secret-key service at api.wordpress.org/secret-key/1.1/salt/, and it returns eight fresh lines each time you open it. Replace the whole block in wp-config.php with the new lines, keeping the define() format exactly as shown. If you have WP-CLI, the same job is one command: wp config shuffle-salts, which rewrites the values in place. Replacing the salts logs every user out, including you, because every login cookie was signed with the old keys. That is exactly what you want after a suspected break-in or a staff change. Generate a fresh set per site, never paste one set into several sites, and rotate the salts after any security incident, even a suspected one.
4. Can I move wp-config.php outside the web root?
Yes, WordPress looks for wp-config.php one directory above the site root if it is not in the root, so moving it there puts it outside the folder the web server can serve. Download the file, upload it one level up, and delete the original from the web root, and WordPress finds it automatically. Check with your host first, because some managed hosts expect the file in the root and will regenerate it there, and confirm the folder above the web root is actually outside it. If you cannot move the file, do not worry: permissions of 400 or 440 and the .htaccess deny rule achieve most of the same protection without the move.
5. Does .htaccess work on nginx hosting?
No. .htaccess is an Apache feature, and LiteSpeed reads it too, but on nginx it is ignored entirely. If your site runs on nginx, the wp-config.php half of this guide still applies in full, because it is PHP, not server configuration, and the .htaccess rules need translating into the nginx server block, usually through the host’s panel or support. The nginx equivalents are straightforward: a location block that denies access to wp-config.php, a location block that denies PHP execution in the uploads folder, and the same security headers set in the server block. Ask your host to translate the rules in this guide, or use a plugin that writes nginx rules, and test each one the same way you would test an .htaccess change.
6. Will DISALLOW_FILE_EDIT break my plugins or updates?
No. DISALLOW_FILE_EDIT only removes the theme and plugin code editors from the dashboard. It does not stop plugins from updating, it does not stop themes from updating, and it does not stop you from installing new plugins. The constant that blocks updates is its stronger sibling, DISALLOW_FILE_MODS, which also disables plugin and theme installation and updates from the dashboard, and that one should only be used on sites where updates run through WP-CLI or a deployment pipeline. If you edit theme files from the dashboard today, you will need to change that habit: edit them in a code editor and upload them instead. That is a better workflow anyway, because the dashboard editor has no version control and no undo.
7. Should I block xmlrpc.php on my WordPress site?
Block it only if nothing on your site uses it. The endpoint is a frequent target for brute-force attacks, because a single request can test hundreds of passwords at once, and blocking it removes that path entirely. But blocking it breaks Jetpack, because Jetpack communicates through XML-RPC, and it breaks the WordPress mobile apps and some older publishing tools. If you use Jetpack or publish from the mobile app, do not block it, or use a selective block that allows Jetpack’s servers and blocks everyone else. If you publish only from the dashboard in a browser and do not use Jetpack, block it, because nothing on your site needs it and the endpoint only attracts attacks. Whatever you decide, test the tools you use after adding the rule.
Conclusion
Two files, eight rules, and an hour of careful work. That is the whole job, and it is the highest-value hour in WordPress security, because the two files decide what an attacker finds once they are inside. Tighten wp-config.php to 400, replace the salts, set the seven constants, move the file above the web root if your host allows it, and delete every stray copy. Then add the .htaccess rules: no directory listing, no direct access to the sensitive files, no PHP execution in uploads, a deliberate decision on xmlrpc, the wp-includes block, and the four safe security headers. Test after every change, keep the backup, and keep the rules above the WordPress block where they will survive.
None of this makes a site unhackable, and this guide does not pretend otherwise. A zero-day in a plugin, a stolen password, or a vulnerability in PHP itself can still reach a hardened site, which is why the rest of the stack still matters: updates installed promptly, strong unique passwords, two-factor authentication on every administrator account, a plugin audit that removes what the site does not use, and a backup plugin that is tested before the day it is needed. WordPress wp-config hardening is a one-hour job that pays for itself the first time an attack fails, because it limits what the attacker can do with the access they gain. A stolen admin session that cannot open the code editor is a nuisance instead of a takeover. An uploaded shell that cannot execute is a file on disk instead of a backdoor. A misconfigured server that cannot serve wp-config.php is an error page instead of a leaked database password.
Do the work in order: backup first, then the file, then the test. Start with the permissions, because they take ten seconds and they work even if every other rule fails. Then the salts, then the constants, then the .htaccess rules, one at a time, testing after each. When the checklist passes and the editors are gone from the dashboard menu, the job is done, and the next post in this series, the Day 30 hub, will gather everything this month has covered into one learning path, from the first HTML tag to the hardened WordPress site you now run.
Related Articles You’ll Love
- WordPress Security Plugin Checklist After PHP2Shell — the full security checklist this guide builds on, including the plugin audit and the update routine.
- WordPress Plugin Vulnerabilities: Audit, Remove, Replace — how to find the vulnerable plugins that hardening alone cannot fix, and what to replace them with.
- PHP Session Security Hardening After CVE-2026-115560 — the session flaws fixed in November 2026, and the debug discipline that keeps session errors out of public view.
- WordPress File Upload Security: Block PHP in Uploads — the upload filter and the server-level block this guide pairs with, including the nginx equivalent.
- WordPress Backup Plugin: Restore Your Site After a Hack — the recovery guide to read before you edit these files, and the restore procedure for the day hardening fails.