
WordPress htaccess Rules: Complete 2026 Blueprint & Practical Guide
Direct Answer (WordPress htaccess Rules): Clean server-level WordPress .htaccess rules execute in under 2 milliseconds before PHP ever boots: back up via SFTP first, place custom rules strictly above # BEGIN WordPress, enforce single-hop HTTPS + canonical non-www redirects, block wp-config.php / xmlrpc.php / directory browsing, send modern security headers (X-Content-Type-Options, Referrer-Policy), and set 1-year immutable caching for WebP, SVG, and WOFF2 assets.
Most WordPress site owners install three separate plugins to handle HTTPS redirects, security headers, XML-RPC blocking, and browser caching—forcing WordPress to boot PHP and query MySQL on every single bot hit just to say “Access Denied.” On Apache and LiteSpeed Enterprise servers (such as Hostinger and Hostnin), a lean 45-line .htaccess configuration handles all of those protections directly at the web server layer in under 2 milliseconds with zero PHP overhead.
Because a single syntax typo in .htaccess triggers an immediate HTTP 500 Internal Server Error, many developers avoid touching it altogether. At Pixora Studio and OrdainIT, clean .htaccess hardening is standard on every LiteSpeed WordPress launch. This 2026 WordPress htaccess rules guide gives you our exact copy-paste production snippets, safe editing SOP, and LiteSpeed compatibility guardrails.
Safe Editing Protocol vs. Triggering a 500 Internal Server Error
Before editing a single line of public_html/.htaccess, follow three non-negotiable precautions:
- Download a Local Backup Copy via SFTP/File Manager: Save
.htaccess.bakto your computer so if any directive is unsupported by your host, re-uploading the original restores HTTP 200 in five seconds. - NEVER Edit Inside
# BEGIN WordPress…# END WordPressor# BEGIN LSCACHE…# END LSCACHE: WordPress and LiteSpeed Cache overwrite everything between their own comment markers automatically! Place all custom security, header, and redirect rules cleanly above# BEGIN WordPress. - Confirm Your Server Reads
.htaccess: Apache and LiteSpeed Enterprise read.htaccessnatively (note: pure Nginx servers ignore.htaccesscompletely—Nginx rules live in servernginx.confblocks instead).
Layer 1: Essential WordPress .htaccess Security Rules (Copy-Paste)
Aligned with the WordPress.org Hardening Guide, add these zero-bloat security rules right at the top of .htaccess:
# 1. Disable Directory Browsing
Options -Indexes
# 2. Protect wp-config.php, .htaccess, .env & error logs
<FilesMatch "^(wp-config.php|.htaccess|.env|debug.log|readme.html)$">
Require all denied
</FilesMatch>
# 3. Block XML-RPC Brute-Force & Pingback Exploits
<Files xmlrpc.php>
Require all denied
</Files>
# 4. Essential Modern HTTP 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>Pair these lightweight origin rules with our Cloudflare Free 5-Rule WAF Setup and Zero-Bloat Security Guide for enterprise defense with zero heavy security plugin bloat.

Layer 2: Single-Hop Canonical HTTPS + Non-WWW 301 Redirect
Avoid wasting crawl budget on chained redirects (http://www.site.com → https://www.site.com → https://site.com). Combine both rules into one single 301 hop directly above # BEGIN WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R=301,L]
</IfModule>If your site sits behind Cloudflare Free with Always Use HTTPS: ON and Page/Redirect Rules handling www at the edge, you can let Cloudflare execute that single hop at the global edge and keep origin .htaccess even cleaner!
Layer 3: Browser Caching & Compression Headers (LiteSpeed Compatible)
| Static Asset Type | Recommended Browser TTL | Performance Benefit |
|---|---|---|
| WebP, AVIF, SVG, JPG, PNG Images | 1 Year (access plus 1 year) | Repeat blog readers load covers & diagrams in 0ms from memory |
| Self-Hosted WOFF2 Web Fonts | 1 Year + immutable | Zero font re-downloads across multi-page sessions (0.00 CLS) |
Versioned CSS & JS (?ver=...) | 1 Year (busted via version string) | Eliminates “Serve static assets with an efficient cache policy” warnings |
| HTML Documents & XML Sitemaps | Never long-browser-cache HTML | Controlled strictly by LiteSpeed Cache server headers so edits show live |
Important LiteSpeed note: When you run LiteSpeed Cache with Browser Cache: ON (31557600 seconds), LiteSpeed automatically writes clean mod_expires rules inside its own # BEGIN LSCACHE block—never duplicate conflicting ExpiresByType blocks manually if LiteSpeed Cache is already managing browser caching!

Bonus Hardening: Block Direct PHP Execution Inside /wp-content/uploads/
Create a tiny separate 5-line .htaccess file directly inside /wp-content/uploads/.htaccess to stop hackers from ever executing an uploaded PHP webshell inside your media directory:
<FilesMatch ".(php|phtml|php5|php7|php8|phar)$">
Require all denied
</FilesMatch>This single 3-line file blocks PHP execution inside uploads while still allowing WebP, SVG, PNG, JPG, and PDF media files to load cleanly at full speed!
Complete .htaccess Rule Verification Table
| Verification Test | Expected Live Result | How to Verify in 10 Seconds |
|---|---|---|
Visit /wp-config.php & /xmlrpc.php | HTTP 403 Forbidden | Test directly in an Incognito browser tab |
Visit /wp-content/uploads/ | HTTP 403 (no file listing) | Confirms Options -Indexes is active |
| Check Security Headers | Grade A / A+ | Inspect Response Headers in Chrome DevTools Network tab |
Test http://www.yourdomain.com | Single 301 → 200 OK | Verify 1 hop only via curl or Redirect Checker |


Need server-level 301 regex rules for a major URL redesign too? Pair this guide with our 301 Redirect Mapping Guide for Website Redesigns and Agency Staging Workflow Guide.
Understanding Exactly How LiteSpeed & Apache Process .htaccess
Every time a visitor, Googlebot, or malicious scanner requests a URL on your WordPress server, LiteSpeed Web Server inspects your root public_html/.htaccess file before launching a single PHP worker or opening a MySQL database connection. That architectural timing is why server-level rules are 50× faster and far more resilient under bot attacks than PHP security plugins:
- Zero PHP Memory Consumption: Blocking 10,000 automated
xmlrpc.phpbrute-force hits via a WordPress security plugin forces PHP to boot WordPress Core 10,000 times—easily maxing out shared hosting entry processes and taking your website offline. Blockingxmlrpc.phpinside.htaccess(or even earlier at Cloudflare Edge WAF) drops all 10,000 requests in under 1 millisecond with 0% PHP CPU usage! - Clean Rule Execution Order Matters: Rules at the top of
.htaccessexecute first. Always place your security file blocks (wp-config.php,.env,xmlrpc.php) and canonical 1-hop HTTPS/non-www 301 redirects above WordPress’s standard# BEGIN WordPresspermalink rewrite block—if you place 301 redirects below# BEGIN WordPress, WordPress’s catch-allindex.phprule intercepts the request first and your custom redirects never fire! - Keep
.htaccessLean Under 150 Lines: LiteSpeed caches parsed.htaccessdirectives efficiently in RAM, yet dumping 4,000 individual IP bans or 2,000 manual single-URL redirects into.htaccessmakes maintenance messy. Move bulk IP/country/bot blocking to Cloudflare Free WAF and use clean regex patterns in.htaccessfor large redirect migrations.
Advanced .htaccess Performance & CORS Snippets for 2026
Beyond basic security and HTTPS canonicalization, three compact snippets solve real-world WordPress font, compression, and hotlinking headaches cleanly:
# 1. Enable Clean CORS for Self-Hosted WOFF2 Web Fonts (Prevents Safari/CDN Font Errors)
<IfModule mod_headers.c>
<FilesMatch ".(woff2|woff|ttf|svg)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
# 2. Prevent Author Username Enumeration via /?author=1 Queries
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^author=([0-9]*)
RewriteRule .* - [F,L]
</IfModule>
# 3. Clean 301 Redirects for Legacy .html URLs During Redesigns
<IfModule mod_rewrite.c>
RewriteRule ^old-service.html$ https://yourdomain.com/service/ [R=301,L]
</IfModule>10-Point .htaccess Audit Checklist Before Every Client Launch
- SFTP Backup Confirmed: Fresh
.htaccess.baksaved locally before any modification. - WordPress Core Block Intact:
# BEGIN WordPress…# END WordPresscompletely untouched at the bottom. - LiteSpeed Cache Markers Clean: No duplicate or conflicting legacy W3 Total Cache / WP Rocket blocks left behind from previous optimization plugins!
Options -IndexesActive: Visiting/wp-content/uploads/returns HTTP 403 Forbidden cleanly.- Sensitive Files Blocked:
wp-config.php,.env,debug.log, andxmlrpc.phpall return HTTP 403. - Uploads PHP Execution Blocked: Separate 3-line
.htaccessinside/wp-content/uploads/blocks direct.phpexecution. - Single-Hop Canonical HTTPS Active:
http://www.yourdomain.comresolves tohttps://yourdomain.comin strictly 1301hop. - Modern Security Headers Verified:
X-Content-Type-Options,X-Frame-Options,Referrer-Policy, andPermissions-Policypresent in Chrome DevTools Response Headers. - WOFF2 & WebP 1-Year Caching Verified: Static media returns long
cache-controlTTLs without breaking dynamic HTML caching. - Full Site Smoke Test Passed: Homepage, WP Admin, Elementor Editor, Sitemap XML, Contact Form, and WooCommerce Checkout all verified HTTP 200 immediately after saving!
Mastering clean, minimal .htaccess engineering lets you strip three redundant security and redirect plugins off every WordPress build—delivering faster TTFB, airtight server hardening, and effortless long-term maintenance.
Coordinating .htaccess Rules With Cloudflare Free & LiteSpeed Cache
Modern WordPress speed stacks run three distinct layers working together—Cloudflare Free at the global edge, LiteSpeed Enterprise reading .htaccess at your origin server, and WordPress PHP only when dynamic pages are generated. Knowing which exact job belongs in each layer prevents frustrating double-redirect loops and conflicting cache headers:
- Let Cloudflare Edge Handle Global Bot Filtering & TLS 1.3: Cloudflare’s 5 free Custom WAF rules drop XML-RPC floods, credential-stuffing bots, and
.envscanners hundreds of miles away before they ever consume your origin bandwidth—while your origin.htaccessrules act as an airtight second defense layer in case anyone ever bypasses Cloudflare and hits your origin IP directly. - Let LiteSpeed Cache Manage Its Own
# BEGIN LSCACHEBlock: Never hand-edit rules inside LiteSpeed’s auto-managed markers, and never paste old WP Rocket or W3 Total Cache Gzip/Expires blocks into a LiteSpeed.htaccessfile—LiteSpeed Enterprise serves Brotli/Gzip and 1-year browser caching natively with zero conflicting directives. - Keep Custom Permanent 301 Redirects Cleanly Above
# BEGIN WordPress: Whenever migrating a redesigned site from legacy.htmlor date-prefixed URLs, server-level regex 301 rules inside.htaccessexecute in under 2 milliseconds without booting WordPress PHP once—protecting crawl budget and Core Web Vitals simultaneously.
Audit your live .htaccess once every six months after cleaning up old plugins to make sure no uninstalled plugin left dead rewrite markers behind.
Always test your live .htaccess rules in an Incognito browser window and verify x-litespeed-cache: hit plus HTTP 403 blocks on sensitive files before closing your SFTP session—keeping server security tight with zero PHP plugin bloat.
Keeping clean SFTP backups and modular server-level security rules outside the WordPress core block protects uptime and TTFB across every future update.
Key WordPress htaccess Rules Takeaways & Quick Action Plan
Putting WordPress htaccess Rules into practice consistently separates high-ranking 95+ speed websites from sluggish builds. Review every core WordPress htaccess Rules benchmark above, verify your live settings on staging first, and re-audit your WordPress htaccess Rules results every quarter for compounding organic growth.
Frequently Asked Questions About WordPress .htaccess Rules
Does LiteSpeed Web Server support Apache .htaccess rules?
Yes! LiteSpeed Enterprise is a 100% drop-in compatible reader for Apache .htaccess directives (mod_rewrite, mod_headers, mod_expires, Require all denied)—executing them significantly faster than traditional Apache.
Why did my site show a 500 Internal Server Error after editing .htaccess?
A 500 error means either a tiny syntax typo or an Apache module your host doesn’t allow in .htaccess (such as old Apache 2.2 Order deny,allow syntax on strict servers, or missing <IfModule> wrappers). Simply restore your SFTP .htaccess.bak copy to recover immediately.
Will WordPress overwrite my custom .htaccess security rules when I save Permalinks?
Only if you mistakenly pasted them inside the # BEGIN WordPress … # END WordPress block! Everything placed cleanly above # BEGIN WordPress stays untouched permanently.
Can I add HSTS (Strict-Transport-Security) inside .htaccess?
Yes, once 100% of your site and subdomains run valid HTTPS reliably—or enable HSTS cleanly inside Cloudflare SSL/TLS → Edge Certificates with a safe initial 6-month max-age.
Can Pixora Studio harden our WordPress .htaccess, Cloudflare WAF, and security headers safely?
Yes! We configure SFTP-backed .htaccess hardening, uploads PHP blocking, single-hop canonical HTTPS, A+ security headers, and Cloudflare Free WAF cleanly with zero downtime. Contact us via our Contact page.
Want Zero-Plugin Server Security & A+ HTTP Headers on WordPress?
Let Pixora Studio harden your .htaccess, LiteSpeed Cache headers, and Cloudflare WAF cleanly without slowing PHP.