WordPress .htaccess Security Rules That Actually Help
The .htaccess rules that shrink WordPress's attack surface: no PHP in uploads, deny wp-config.php, no directory listings, and what each 500 error means.
TL;DR: Three .htaccess rules do most of the work: block PHP execution in wp-content/uploads, deny direct access to wp-config.php, and turn off directory indexing. Two more help in specific cases: closing xmlrpc.php when nothing on your site uses it, and WordPress’s own block for include-only scripts in wp-includes. Hiding your version, bad-bot blocklists and author redirects are marginal. The tables below show which is which, the real HTTP status codes from a test server, and what each kind of 500 error means.
Most of what gets sold as “WordPress .htaccess security” is padding. The rules that change your attack surface are short. The popular extras, such as hiding your WordPress version or pasting a 200-line bad-bot user-agent list, range from marginal to pure theatre. Below is which is which, and why.
| Rule | Effort | Real risk reduced |
|---|---|---|
Deny PHP execution in /uploads | One-time, paste once | High: stops an uploaded web shell from executing |
Deny wp-config.php | One-time, paste once | Low-probability but severe: guards the failure case where PHP stops running and the file gets served as plain text |
| Disable directory indexing | One-time, paste once | Low but real: stops stray backups and SQL dumps from being listed |
Block xmlrpc.php when nothing uses it | One-time | Real: WordPress’s brute-force guide calls it a frequent target. Breaks Jetpack and the mobile apps |
WordPress’s wp-includes block | One-time | A second layer: refuses direct requests to scripts that are only meant to be included |
| Hide WordPress version / generator tag | A few minutes | About none: versions show up anyway in asset query strings and other markup |
| Giant bad-bot user-agent blocklist | Ongoing upkeep, stale immediately | About none, and Apache checks every pattern on every request |
IP-lock wp-login.php | Ongoing upkeep | Strong with a static IP; locks you out when your IP changes |
Author-enumeration redirect (?author=1) | A few minutes | Incomplete: the /wp-json/wp/v2/users REST endpoint still shows user slugs |
Use the WordPress .htaccess generator to assemble the root-file rules. It writes Apache 2.4 syntax. The uploads rule goes in a separate file, so check it matches the extensions shown below. See WordPress .htaccess redirect rules if what you actually need is 301s, not hardening.
One thing to settle first: .htaccess only does anything on Apache (and LiteSpeed, which reads it too). On nginx it is ignored entirely. The file just sits there while you believe you’re protected. If your host runs nginx, none of this applies and you need server/location blocks instead. curl -I https://yoursite.com often shows the server in the Server: header, but behind a CDN that header names the CDN, so ask your host if it’s unclear.
One more scope note: this guide is only about shrinking attack surface. If you’re in .htaccess because you just moved to HTTPS and the browser is throwing mixed-content warnings, that’s a different job. See clearing WordPress mixed content after moving to HTTPS for the force-HTTPS rewrite rule and the database cleanup it doesn’t do on its own.
The rule that matters most: no PHP in /uploads
This is the one worth doing. The web server can write to wp-content/uploads by design: every media upload, and every plugin that saves a file, writes there. If an attacker gets a PHP file into that directory (through a vulnerable upload handler, an image field that doesn’t check the file type, or a compromised plugin), the difference between an annoyance and full remote code execution is whether the server will run that file when requested. Deny execution and the payload is just an inert file on disk.
Drop this into wp-content/uploads/.htaccess (create the file if it doesn’t exist):
<FilesMatch "\.(php|phtml|phar)$">
Require all denied
</FilesMatch>
Why three extensions and not just .php: on Debian and Ubuntu’s stock mod_php setup, PHP runs any file ending in .php, .phtml or .phar. The handler in Ubuntu’s php8.3.conf matches ".+\.ph(?:ar|p|tml)$". We tested this on 2026-11-16 with Apache 2.4.58 and PHP 8.3.6 from Ubuntu 24.04’s packages, using that stock handler file. With a \.php$ rule, test.php returned 403, but test.phtml and test.phar both returned 200 and printed “PHP ran”. With the rule above, all three returned 403. Other hosts map other extensions to PHP, so check your handler config if you’re unsure.
This is Apache 2.4 syntax. The older Order Deny,Allow / Deny from all lines only work on 2.4 when mod_access_compat is loaded, and Apache’s upgrade guide discourages mixing the old and new styles. The 500 table further down shows what happens when the module is missing.
Protect wp-config.php
wp-config.php holds your database credentials and auth salts (our wp-config.php guide covers the file itself). While PHP is running, a direct request to it returns a blank page, because PHP executes the file instead of printing it. The risk is the failure case: if PHP stops handling .php files, for example after a broken migration or a removed handler, Apache can serve the file as plain text and show your database password to anyone who asks. WordPress’s Hardening page gives this rule and says to put it at the very top of .htaccess:
<Files wp-config.php>
Require all denied
</Files>
Turn off directory indexing
If someone visits a folder with no index.php and Apache has Options +Indexes on, it lists the contents: every backup file, every stray SQL dump you forgot about. Turn it off site-wide:
Options -Indexes
Low severity, but real, and it costs nothing.
That’s the core list. You can assemble these with the WordPress .htaccess generator rather than copy-pasting from a forum post written for an old Apache version.

The same result as a table, for anyone who can’t read the screenshot above (screen readers, AI crawlers, and anyone skimming):
| Request | Before the 3 rules | After the 3 rules |
|---|---|---|
GET /wp-content/uploads/test.php | 200: the PHP file executes | 403 Forbidden |
GET /wp-content/uploads/ (directory listing) | 200: files in the folder are listed | 403 Forbidden |
GET /wp-config.php | Not part of this before/after pair. The risk shows up only when PHP stops running (see above), not under normal operation | 403 Forbidden |
GET / (homepage) | 200 | 200, unaffected |
Same test server, same note: Apache 2.4.58, PHP 8.3.6, hand-made test files, no full WordPress install. That run (2026-11-01) used a \.php$ uploads rule; the .phtml and .phar check above came later.
Two optional rules
Close xmlrpc.php if nothing uses it. WordPress’s brute-force guide says: “xmlrpc.php is a frequent brute‑force target (especially the system.multicall method). If you don’t use XML‑RPC, disable it. If you do (e.g., Jetpack, mobile apps), restrict it (WAF rules) and rate‑limit aggressively.” On Apache:
<Files xmlrpc.php>
Require all denied
</Files>
This also stops incoming pingbacks, which arrive through the same file. It is not a full brute-force defence, because wp-login.php takes the same guesses (see below).
WordPress’s block for include-only scripts. The Hardening page offers this as “a second layer of protection” for scripts no visitor should request directly. It uses mod_rewrite, so it goes outside the WordPress markers, above # BEGIN WordPress:
# Block the include-only files.
<IfModule mod_rewrite.c>
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]
</IfModule>
The same page warns that it “won’t work well on Multisite”, because the ^wp-includes/[^/]+\.php$ line stops ms-files.php from serving images. Leaving that line out makes it work there, with less protection. We have not tested this block.
Why a rule gives a 500 error, and how to read it
A rule only runs if the main Apache config allows that kind of directive in .htaccess. AllowOverride sets that, per class. When a line isn’t allowed or isn’t known, Apache returns 500 for every URL the file covers and writes the reason to the error log. We checked each case on 2026-11-16 (Apache 2.4.58, PHP 8.3.6, a one-line index.php in place of WordPress):
Line in .htaccess | What it needs | What we saw without it |
|---|---|---|
The WordPress block (RewriteRule, RewriteCond) | AllowOverride FileInfo, plus Options FollowSymLinks or SymLinksIfOwnerMatch | With FileInfo the post URL worked (200). With both symlink options off, every URL returned 403 and the log showed “AH00670: Options FollowSymLinks and SymLinksIfOwnerMatch are both off” |
Require all denied inside <Files> or <FilesMatch> | AllowOverride AuthConfig | 500 on every URL; log: “Require not allowed in <Files> context” |
Options -Indexes | AllowOverride Options | 500 on every URL; log: “Options not allowed here” |
Order allow,deny / Deny from all | mod_access_compat loaded | 500 on every URL; log: “Invalid command ‘Order’, perhaps misspelled or defined by a module not included in the server configuration”. With the module loaded, the same lines returned 403 for wp-config.php |
php_value memory_limit 256M | PHP running as mod_php | Without mod_php, as on PHP-FPM setups: 500; log: “Invalid command ‘php_value’”. Our memory exhausted guide covers the .user.ini alternative |
<Directory> | Never allowed in .htaccess | Not tested. Apache’s docs allow it only in the server config and virtual hosts |
With AllowOverride FileInfo AuthConfig Options (or All), the WordPress block and all three core rules worked together: homepage 200, post URL 200, wp-config.php 403, the uploads folder 403, uploads/test.php 403.
What NOT to do
Treating a closed xmlrpc.php as your brute-force defence. XML-RPC’s system.multicall method lets an attacker bundle many login guesses into one request, which is why WordPress’s brute-force guide calls it a frequent target. But wp-login.php takes the same guesses one request at a time, and blocking xmlrpc.php does nothing for that. The same guide says to “Apply rate limits to /wp-login.php and /xmlrpc.php globally”, and recommends strong passwords, two-factor login and passkeys. Close xmlrpc.php if you don’t need it, then add those.
Hiding your WordPress version / removing the generator tag. Stripping readme.html and the <meta name="generator"> tag feels like hardening. The version still shows up in the ?ver= query strings on core scripts and styles, and in other markup. You’re not hiding much, and keeping WordPress updated matters far more.
Giant bad-bot user-agent and referrer blocklists. User agents are a single spoofed HTTP header. These lists are stale the day you paste them, they block nothing competent, and Apache checks every pattern on every request. WordPress’s brute-force guide says that older advice like “heavy .htaccess rewrites, country IP blocklists” is now “either unreliable, break AJAX‑based plugins, or degrade user experience”. Skip them.
IP-locking wp-login.php without a static IP. WordPress’s brute-force guide gives the rule as an example (<Files "wp-login.php"> with Require ip and your addresses). It works well until your ISP rotates your address and you’ve locked yourself out of your own admin. Only use it with an IP that doesn’t change.
Author-enumeration redirects (?author=1) on their own. The .htaccess rewrite people paste for this leaves the REST API open. Reading the WordPress 7.1.4 source (not run), /wp-json/wp/v2/users lists, to anyone who can’t manage users (visitors who aren’t logged in included), every user who has published a post in a post type shown in the REST API. Each entry has a slug, which WordPress builds from the login name unless someone changed it. Blocking one path while the other stays open is theatre.
Still stuck?
If a rule 500s the site, remove the last block you added and reload; that brings the site back and isolates the line. Then read the error log and match the message against the 500 table above. Most of the time it’s a line AllowOverride doesn’t permit, not a typo. If a rule seems to do nothing, confirm you’re on Apache and that AllowOverride isn’t None for the directory (many managed hosts restrict it). Build the file from a known-good template with the .htaccess generator, keep the rules that matter, and drop the rest.
If the whole file is missing or corrupted, and every post 404s but the homepage still works, see restoring WordPress’s default .htaccess file to get permalinks routing again before adding any of the rules above. If the file exists but the same 404-on-posts symptom shows up after a migration or a host change, the rewrite rules may just be stale, or the new server may ignore the file. See WordPress posts returning 404 while the homepage works for that case.
Sources
- WordPress Advanced Administration: Hardening WordPress, Brute Force Attacks
- WordPress REST API Handbook: Users reference
- WordPress source code: class-wp-rest-users-controller.php, user.php
- Apache HTTP Server 2.4: .htaccess files, AllowOverride, Options, FilesMatch, Require, mod_access_compat, Upgrading to 2.4 from 2.2
William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.
FAQ
Questions
How do I stop PHP files from running in the WordPress uploads folder?
Create a .htaccess file inside wp-content/uploads with a FilesMatch block matching .php, .phtml and .phar files and Require all denied. The web server can write to uploads by design, so denying execution means a malicious PHP file that gets in there sits on disk as an inert file instead of becoming remote code execution.
Do I need to block direct access to wp-config.php?
Yes, and it costs nothing. Add a Files block for wp-config.php with Require all denied. While PHP is running, a direct request just returns a blank page, but if PHP stops handling the file, for example after a broken migration, Apache can serve it as plain text and expose your database credentials.
Why did my site show a 500 Internal Server Error right after I edited .htaccess?
Apache found a line it isn't allowed to run or doesn't know. Usual causes: an Options or Require line that AllowOverride doesn't permit, an old Order or Deny from all line without mod_access_compat, or php_value on a PHP-FPM server. The error log names the line. Remove the last block you added to bring the site back.
Does .htaccess work on nginx?
No. .htaccess only does anything on Apache, and on LiteSpeed, which reads it too. On nginx the file is ignored entirely, so it sits there while you believe you are protected. Ask your host which server runs your site, then use nginx server or location blocks instead.
Does blocking xmlrpc.php stop WordPress brute force attacks?
It closes one route. WordPress's brute-force guide calls xmlrpc.php a frequent target, especially its system.multicall method, and says to disable it if you don't use XML-RPC. Guesses can still go to wp-login.php, so you also need rate limits, strong passwords and two-factor login. Jetpack and the mobile apps need XML-RPC.