Categories
WordPress

The Almost Perfect .htaccess File for WordPress (2026 Update)

The default WordPress .htaccess file plus the snippets worth adding in 2026: HTTPS, redirects, compression, caching, security headers, and what to do on NGINX hosts like WP Engine and Pressable.

Last updated: October 17, 2026

Disclosure: some links in this post are affiliate links, marked as sponsored. If you buy through them I may earn a commission at no extra cost to you. I only recommend hosts I have used on real client sites.

Years ago I wrote an article called “Almost perfect htaccess file for WordPress.” It was never perfect, but it was popular. Tens of thousands of views later (and $0 earned), I brought it back in 2023. This is the 2026 version, with the old Apache 2.2 syntax replaced and a new section for hosts that ignore .htaccess entirely.

Does your host even read .htaccess?

Check this first, because it decides whether anything below matters. The .htaccess file is an Apache feature. If your host runs NGINX, the file sits there and does nothing.

  • Apache hosts (most shared hosting, cPanel servers, many VPS setups) read .htaccess on every request.
  • WP Engine has deprecated .htaccess. Its support article points you to Web Rules, Redirect Rules, and PHP for headers instead.
  • Pressable runs NGINX, so .htaccess rules are ignored. Redirects go in a custom-redirects.php file, which I cover in my Pressable redirects guide.

If you are on one of those managed hosts, skip to the NGINX section near the end.

The default WordPress .htaccess file

Before adding anything, know what should be there. This is the block WordPress writes when you save your permalinks. It comes from the WordPress .htaccess documentation.

# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

WordPress rewrites everything between the BEGIN and END markers when permalinks are saved. Put your own rules above # BEGIN WordPress so they survive.

Snippets worth adding

I can’t tell you exactly what your site needs, but these are the rules I reach for most. Add one at a time and test, because a single typo in .htaccess gives you a 500 error on every page.

1. Turn off directory browsing

Most hosts already do this. It costs nothing to be sure.

Options -Indexes

2. Force HTTPS

Free certificates from Let’s Encrypt mean there is no excuse for plain HTTP in 2026. Many hosts have a toggle for this. If yours doesn’t:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. Simple 301 redirects

I still prefer the Redirection plugin because it logs hits and 404s. For a quick one-off, .htaccess works:

Redirect 301 /old-page/ https://example.com/new-page/

4. Block an IP address

The 2023 version of this post used the old Deny from syntax. Apache 2.4 uses Require:

<RequireAll>
  Require all granted
  Require not ip 203.0.113.45
</RequireAll>

For anything beyond a handful of IPs, block at the edge with Cloudflare or a security plugin instead.

5. Compression

The old post used mod_gzip, which is long dead. Use mod_deflate:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
  AddOutputFilterByType DEFLATE application/javascript application/json image/svg+xml
</IfModule>

If you use Cloudflare or a host with its own CDN, compression is usually already handled there.

6. Browser caching for static files

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</IfModule>

7. Basic 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"
</IfModule>

Only add Strict-Transport-Security once you are sure every subdomain works over HTTPS. It is hard to undo.

8. Protect wp-config.php

<Files wp-config.php>
  Require all denied
</Files>

9. Block xmlrpc.php

Unless you use the Jetpack mobile app or another service that needs XML-RPC, turn it off:

<Files xmlrpc.php>
  Require all denied
</Files>

Jetpack itself needs XML-RPC to talk to WordPress.com, so test after adding this if you run Jetpack.

Other .htaccess options

These come up a lot. I don’t add them by default, but they are worth knowing about:

  • Password protecting wp-admin with HTTP basic auth
  • Stopping username enumeration through ?author= URLs
  • Preventing image hotlinking
  • Forcing certain file types to download
  • Blocking direct access to files in wp-includes

My WordPress security best practices post covers which of these still matter.

On NGINX hosts: where these rules go instead

On a managed host that ignores .htaccess, each rule above has a home somewhere else:

RuleWP EnginePressable
HTTPS redirectFree SSL, then the “Secure all URLs” option on the SSL pageFree SSL, then set your site URLs to https
301 redirectsRedirect Rules in the User Portal, or the Redirection plugincustom-redirects.php or the Redirection plugin
Block an IPWeb Rules (access rules)Ask support, or use Cloudflare
Compression and cachingDone by the platformDone by the platform and Edge Cache
Security headersPHP via the send_headers hookPHP via the send_headers hook

That is one of the quiet perks of managed hosting. Most of the performance rules in this post are already done for you, and you stop babysitting a config file. If you are weighing a move, I compare the two hosts in Pressable vs WP Engine.

Done editing config files? WP Engine handles caching, compression, HTTPS, and redirects from its dashboard, so .htaccess stops being your problem.

FAQ

Where is the .htaccess file in WordPress?

It sits in the root folder of your WordPress install, next to wp-config.php. It is a hidden file, so turn on “show hidden files” in your FTP or SFTP client to see it.

Does WP Engine use .htaccess?

No. WP Engine has deprecated .htaccess and recommends Web Rules, Redirect Rules in the User Portal, and PHP for custom headers.

Does Pressable use .htaccess?

No. Pressable runs NGINX, which ignores .htaccess. Redirects go in a custom-redirects.php file or a plugin like Redirection.

Why did my site break after editing .htaccess?

A syntax error in .htaccess causes a 500 Internal Server Error. Remove the last rule you added, or restore the default WordPress block, and the site will come back.

Should I use mod_gzip or mod_deflate?

Use mod_deflate. mod_gzip was an Apache 1.3 module and is not used on modern servers.

Related posts

Tell me why I'm wrong...