WordPress .htaccess Security: What Actually Helps
Practical WordPress .htaccess hardening with Apache snippets, 2.4 notes, testing and rollback, plus clear warnings for Nginx and LiteSpeed.
Topics Hardening & checklists
Practical WordPress .htaccess hardening with Apache snippets, 2.4 notes, testing and rollback, plus clear warnings for Nginx and LiteSpeed.
Topics Hardening & checklists
.htaccess is an Apache config file that can block paths, force HTTPS, and tighten directory access. It is useful on Apache hosts. It is easy to break a site with a bad rule. It is not a substitute for updates, strong logins, or malware cleanup.
.htaccess support.htaccess (download a copy before every change)Nginx-only hosts ignore .htaccess. Equivalent rules belong in the server block or a host panel. Do not paste these snippets on Nginx and expect them to work.
LiteSpeed often honors many Apache rules, but syntax and modules still differ. Change one rule at a time and test.
Managed WordPress hosts (Kinsta, WP Engine, Flywheel-style stacks, and similar) often block or ignore custom .htaccess edits for security. You may only get HTTPS forcing, redirects, or IP rules through the host panel. Ask support before you assume Apache snippets will apply.
OpenLiteSpeed / hybrid stacks may parse different directive syntax. A rule that works on classic Apache 2.4 can 500 on another layer.
WordPress also writes its own rewrite block into .htaccess for pretty permalinks. Keep the # BEGIN WordPress / # END WordPress block intact unless you know why you are changing it.
.htaccess is the wrong tool| Situation | Better approach |
|---|---|
| Nginx or managed host without Apache overrides | Host panel, server block, or CDN rules |
| Blocking exploit-shaped requests at scale | Cloud Firewall or CDN WAF |
| Login brute force | Login protection instead of IP-only wp-login blocks |
| Malware already on disk | Cleanup and scans; .htaccess does not delete infected PHP |
| PHP upload limits | Host php.ini or panel, not always php_value in .htaccess |
Hand-written rules complement a maintained firewall. They do not replace updates or login hardening.
.htaccess/wp-admin/Never edit .htaccess in Word or a rich-text editor. Use a plain-text editor.
Older tutorials use Order allow,deny / Deny from all. Apache 2.4 prefers Require:
# Apache 2.4+
<Files "wp-config.php">
Require all denied
</Files>
# Legacy Apache 2.2 style (some hosts still accept this)
<Files wp-config.php>
Order allow,deny
Deny from all
</Files>
If a 2.2-style rule 500s on your host, switch to the Require form. If you are unsure which Apache version you have, ask the host or check their docs.
Place most of these in the site root .htaccess next to wp-config.php, unless a note says otherwise. Replace example IPs and domains with yours.
wp-config.php<Files "wp-config.php">
Require all denied
</Files>
Options -Indexes
Create (or edit) wp-content/uploads/.htaccess:
<FilesMatch "\.php$">
Require all denied
</FilesMatch>
Test that normal image URLs still load. Some hosts already enforce this; duplicate rules are usually fine.
XML-RPC is used by some mobile apps, Jetpack-related flows, and remote publishing. Disable only when nothing you use needs it:
<Files "xmlrpc.php">
Require all denied
</Files>
Prefer firewall rate limits when you must keep XML-RPC. See login security.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Confirm the site already has a working certificate before forcing HTTPS.
wp-login.php by IPUseful for a fixed office IP. Painful for travel, home ISP changes, and agencies:
<Files "wp-login.php">
Require ip 203.0.113.10
</Files>
Whitelist every IP that must log in, or you will lock yourself out. Login rate limits in a plugin are usually safer for teams. Login protection covers that without IP pinning.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} author=\d [NC]
RewriteRule ^ - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC]
RewriteRule \.(jpe?g|png|gif|webp)$ - [F,NC,L]
</IfModule>
Replace example.com with your domain. Hotlink blocking is bandwidth control more than malware defense.
php_value upload limits only work when PHP runs as an Apache module. Many hosts ignore them or error. Set upload limits in the host panel or php.ini instead.X-Frame-Options) are fine when mod_headers is enabled. A wrong header line can 500 the site; test after adding.wp-login.phpFor those jobs, use hardening, login security, and a real firewall.
Security Ninja Pro’s Cloud Firewall blocks known bad IPs (600M+), filters abusive requests, and handles login/404 abuse without you maintaining a private rulebook. Hand-written .htaccess rules can complement that for host-specific file protection. They should not compete with it by duplicating WAF logic you do not understand.
Setup path: security plugin setup. Conflict advice: plugin conflicts.
.htaccess to .htaccess.broken or restore your downloaded copy.htaccess is a scalpel, not a full security stack. Use a few proven file and directory protections, keep a restore path, and put day-to-day blocking in a maintained firewall. Start Free on WordPress.org or see pricing for Pro.
Found this useful? Share it.