Skip to content

WordPress .htaccess Generator

The rules WordPress needs, plus the hardening rules it does not ship with

Runs in your browser. Nothing is uploaded.

WordPress block

WordPress permalink rules

The rules WordPress needs for pretty permalinks. If posts 404 but the homepage works, a missing block is the first thing to check. A network uses its multisite block instead of the single-site one, never both.

Redirects
Hardening
.htaccess

For Apache. nginx ignores .htaccess entirely, so on an nginx host these rules belong in the server config. Apache itself ignores the file where AllowOverride is set to None.

About the WordPress .htaccess Generator

Generate a WordPress .htaccess: the official rewrite block for a single site or network, HTTPS and www redirects, wp-login IP limits and wp-config protection.

The generator writes a complete .htaccess for Apache: the rewrite block WordPress needs for pretty permalinks, for a single site or a multisite network, plus the redirect and hardening rules you tick. Custom rules always come out above the WordPress block. If you only need the stock file back, the default WordPress .htaccess guide shows it and explains each line.

Worked example: HTTPS, no www, basic hardening

Input. The domain was pasted the way people often copy it from the browser; everything not listed was left at its default.

WordPress permalink rules:            Single site
WordPress folder (optional):          (empty)
Force HTTPS:                          on
Redirect www to non-www:              on
Your domain:                          https://www.Example.com/
Block direct access to wp-config.php: on
Disable directory listing:            on
Block PHP execution in /uploads:      on
Block xmlrpc.php:                     off
Block image hotlinking:               off
Restrict wp-login.php to these IPs:   (empty)

Output, from running the tool's own script in Node.js with that input on 16 November 2026.

# Generated at odiethemes.com/tools/wordpress-htaccess-generator

# ===================================================================
# Keep custom rules ABOVE the WordPress block below. On a single site,
# WordPress rewrites everything between its BEGIN/END markers when it
# saves permalinks, so anything you put in there is silently deleted.
#
# The Require and Options lines need AllowOverride AuthConfig and Options
# (or All). Where the host allows less, every page returns a 500 error.
# ===================================================================

# --- Redirects ---
<IfModule mod_rewrite.c>
RewriteEngine On
  # www -> non-www, straight to https
  RewriteCond %{HTTP_HOST} ^www\.example\.com(:[0-9]+)?$ [NC]
  RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
  # Force HTTPS
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

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

# --- No directory listing ---
Options -Indexes

# --- No PHP execution in /uploads ---
# Put this in wp-content/uploads/.htaccess, NOT in the root file.
# It also covers .phtml and .phar: Ubuntu's stock PHP setup runs those too.
# <FilesMatch "\.(php|phtml|phar)$">
#   Require all denied
# </FilesMatch>

# BEGIN WordPress
<IfModule mod_rewrite.c>
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]
</IfModule>
# END WordPress

Section by section:

  • Lines starting with # are comments, and Apache skips them. The tool reduced the pasted domain to example.com: it dropped https://, www. and the slash, and lowercased it.
  • The redirects only run where mod_rewrite is loaded; <IfModule mod_rewrite.c> skips them otherwise. The first rule sends any request for www.example.com to https://example.com with the same path, as one permanent (301) redirect. The (:[0-9]+)?$ at the end of its condition means the host must be exactly www.example.com, with or without a port number, so a longer name such as www.example.com.au is left alone. The second sends any request that came in over plain HTTP to the https:// address of the same host. Apache's documentation says the query string is passed through unchanged, so ?ref=1 and the like survive. The www rule comes first so that http://www.example.com/ reaches https://example.com/ in one redirect instead of two.
  • <Files wp-config.php> with Require all denied answers direct web requests for the file with 403. PHP still reads it from disk, so WordPress keeps working. In our Apache 2.4.58 test (November 2026, PHP 8.3 as an Apache module, with a one-line test page instead of a WordPress install), /wp-config.php returned 403 with this rule.
  • Options -Indexes stops Apache listing the files in a folder that has no index file. Apache's documentation for the Options directive describes that listing. Our own Apache test did not check the listing with this rule alone.
  • The uploads rule is commented out because it belongs in a separate file, wp-content/uploads/.htaccess. Copy it there and remove the # signs. In our test, the common \.php$ pattern blocked test.php but still let test.phtml and test.phar run as PHP under Ubuntu's stock PHP handler. The three-extension pattern blocked all three.
  • The WordPress block is the one WordPress itself writes for a site at the domain root. It passes the Authorization header on to PHP, leaves index.php alone, and sends every request that is not a real file or folder to /index.php, which works out which post or page was asked for.

When to use it

  • Posts and pages return 404 but the home page works: the WordPress block is often missing or wrong. The permalinks not working guide goes through the other causes too.
  • You want the hardening rules without writing them by hand. The .htaccess hardening guide explains each rule and what it does not protect against.
  • You are moving a site to HTTPS. Set both addresses in Settings > General to https:// first; the mixed content guide covers the rest of the move.
  • WordPress lives in a folder, such as example.com/blog. Enter blog in the folder field and the tool sets RewriteBase and the last rule to /blog/. In our Apache test, the unchanged root block sent /blog/2026/10/post-name/ to the root index.php; the edited block sent it to /blog/index.php.

For one-off redirects from old URLs, see the .htaccess redirect rules guide. They also go above the WordPress block: in our test a rewrite rule placed after it never ran.

Before you upload it

  1. Download the current .htaccess from the WordPress folder and keep a copy. It may hold rules from your host or a caching plugin that you want to keep.
  2. Merge, do not overwrite: paste the new custom rules above any existing # BEGIN WordPress block.
  3. Load the home page, a post and wp-admin straight away. A 500 error on every page means Apache rejected a line. The server's error log names it; in our test it said "Require not allowed in <Files> context" when the host allowed only FileInfo overrides. Remove that block, or ask the host to allow it.

Limits

  • Apache only. nginx does not read .htaccess at all, and Apache ignores it in folders where AllowOverride is None. In our test with AllowOverride None, a line of nonsense in the file still gave a normal page.
  • The Force HTTPS rule checks whether the request reached Apache over HTTPS. Behind a proxy or CDN that ends HTTPS itself, Apache only sees plain HTTP and redirects every request, so the browser goes round in a loop. In our Apache test, the same check written as RewriteCond %{HTTPS} !=on returned a 301 even when the request carried X-Forwarded-Proto: https. On such a setup, use the HTTPS setting of the proxy or host instead.
  • Tick the www redirect only if the Site Address in Settings > General has no www. If it does, WordPress redirects the bare address back to www and the two rules loop. We read this from WordPress 7.1.4's canonical redirect code, which switches between www and no-www to match the Site Address; it was not run.
  • The wp-login.php IP rule checks the address Apache sees. Behind a proxy or CDN that is the proxy's address, unless the server is set up to read the visitor's real address. Use static addresses only, or you lock yourself out when yours changes.
  • On a multisite network, WordPress does not rewrite the file for you, so the network block stays as you paste it. The single-site warning in the output does not apply there.
  • The network blocks assume the WordPress core files sit in the site's own folder. If they are in a subfolder, such as example.com/wp/, the Network Setup screen adds that folder to the targets of the wp-content and .php rules. We read this from WordPress 7.1.4's network.php; it was not run. In that setup, copy the block from the Network Setup screen instead.
  • If you tick the www or hotlink rule without entering a domain, the rules use example.com and the output says so. Replace it before you upload: a hotlink rule left on example.com would block your images on your own pages, because their referrer is your domain, not example.com.

Sources

Guides for this tool

FAQ

Questions

Why did my custom .htaccess rules disappear?

Because they were inside the '# BEGIN WordPress' block. On a single site, WordPress rewrites everything between its BEGIN and END markers whenever it saves its rewrite rules, for example when you save Settings > Permalinks, and anything you added there is lost. Put custom rules ABOVE that block.

Does blocking xmlrpc.php stop brute-force attacks?

It closes one route, not all of them: attackers can still try passwords on wp-login.php. Block it only if nothing you use needs it. Jetpack does, because it connects to your site through xmlrpc.php.

My host runs nginx. Does .htaccess work?

No. nginx does not read .htaccess files, so the file sits there doing nothing and the rules have to go into the nginx server config. Apache can ignore the file too, when AllowOverride is set to None for that folder. If your rules seem to have no effect at all, check those two things before debugging the rules.