Skip to content

WordPress 404 errors: find the layer that caused it, then fix it

Every page 404s, or just one does. Work down the stack — CDN, web server, rewrite rules, post status, custom post types, plugin routes — with the exact command and expected output at each layer.

Your WordPress pages return 404 and you need them working again. This is the repair guide. A 404 is not one bug: four layers can produce it, each leaves a distinguishable trace, and the fix at each one is different. Two commands tell you which layer you are dealing with, and everything after that is targeted. Work in the order below, because the cheapest checks catch the most common causes: rewrite rules never flushed, an overwritten .htaccess, an nginx try_files line that never hands the request to PHP, a trashed or renamed post, a custom post type registered too late, a plugin route whose parent page vanished, or a CDN still serving a 404 it cached an hour ago.

What you’ll accomplish

  • Identify which of four layers emitted the 404, from one curl call and one look at the body.
  • Restore pretty permalinks site-wide when everything except the homepage is broken.
  • Fix single-URL 404s at their real source: post status, slug collision, or a stale negative cache.
  • Get custom post types and plugin routes (WooCommerce endpoints, LMS and membership URLs) resolving again.
  • Finish with a verification sweep across every published URL, so you know the fix held.

Prerequisites

  • WordPress 5.9 or newer. The wp rewrite, wp post, and wp option subcommands used here have been stable across the whole 6.x line.
  • Shell access with WP-CLI. Without it the admin equivalents exist: Settings → Permalinks → Save Changes performs a rewrite flush, and the post list with the All / Trash filters answers the status questions. You still want curl from your own machine.
  • Know which web server you are on. Apache and LiteSpeed read .htaccess; nginx never does, so that advice is noise there and the routing lives in the server config.
  • Know what sits in front of WordPress: Cloudflare, a host CDN, a page cache plugin, or a reverse proxy. Any of them can hold a 404 after the origin is fixed.
  • A staging copy if you have one. Flushing rewrite rules is reversible; editing a live nginx config or .htaccess is not, and a syntax error takes down the whole site.

How it works

A URL like /2026/03/my-post/ is not a file. It is a string that four separate systems interpret in sequence, and any one of them can decide the answer is 404.

The CDN or edge cache looks first, and a 404 is just as cacheable as a 200. Next the web server has to recognise that no file exists at that path and hand the request to index.php anyway — the .htaccess block on Apache, the try_files directive on nginx. If it does not, it answers 404 from its own error page and PHP never runs.

Only then does WordPress get involved. It compares the path against the regular expressions stored in the rewrite_rules option, and the matching rule fills in query variables like name and year. Finally WP_Query runs those variables against the database. If nothing comes back, or what does is not publicly viewable, WordPress sets is_404() and loads the theme’s 404.php.

Four layers of a WordPress request — CDN edge, web server, PHP rewrite rules, and the query — with the failure signature each one leaves when it produces the 404

The practical consequence: the browser shows the same “not found” regardless of which layer answered, but the response headers and body do not. A bare server error page means the request never reached PHP. Your own theme’s 404 page means WordPress ran and made a decision. That distinction halves the search space before you change anything.

Step-by-step

Start at whichever branch matches your symptom rather than running every command in sequence.

Decision tree routing the everything-404s, one-page-404s, and broke-after-a-change symptoms to the layer that owns the fix

  1. Establish the blast radius. One URL and site-wide are different problems with different fixes.
curl -sI https://example.com/ | head -1
curl -sI https://example.com/2026/03/my-post/ | head -1

Expected: HTTP/2 200 from the first, HTTP/2 404 from the second. If the homepage is also failing with a 5xx, stop — that is not a 404 problem. If the homepage is fine and everything else 404s, jump to step 5.

  1. Rule out the edge before you touch the origin. A unique query string forces most CDNs to miss.
curl -sI "https://example.com/2026/03/my-post/?cb=$(date +%s)" | grep -iE '^(http|server|age|x-cache|cf-cache-status)'

Expected: a status line plus headers such as cf-cache-status: MISS or x-cache: MISS. If the cache-busted URL returns 200 while the plain URL returns 404, the origin is already correct and your only job is the purge in step 12. If both return 404, the origin is producing it — continue.

  1. Tell a WordPress 404 apart from a server 404. WordPress renders your theme, which loads assets from wp-content. A server error page does not.
curl -s "https://example.com/2026/03/my-post/?cb=1" | grep -ci 'wp-content'

Expected: a number greater than 0 if WordPress produced the page, 0 if the web server answered on its own. A 0 sends you straight to step 8 (Apache) or step 9 (nginx); anything higher means PHP ran, so continue.

  1. Confirm the content exists and is publicly viewable. Draft, pending, future, private, and trashed posts all return 404 to a logged-out visitor.
wp post list --post_status=any --name=my-post --fields=ID,post_title,post_name,post_status,post_type

Expected: one row showing post_status as publish. A status of trash or draft is your answer — wp post update 123 --post_status=publish returns Success: Updated post 123. and the URL comes back. An empty table means the slug is not what you think; search with wp post list --post_status=any --s=my-post --fields=ID,post_name,post_status. Two posts sharing a slug is the other common finding: WordPress suffixes the newer one, so the URL you remember now belongs to the older post or to nothing.

  1. Read the permalink structure and the stored rewrite rules. This is the single most common cause of “everything except the homepage 404s”.
wp option get permalink_structure
wp option get rewrite_rules --format=json | head -c 200

Expected: something like /%year%/%monthnum%/%postname%/ from the first, and a long JSON object from the second. If the second prints Error: Could not get 'rewrite_rules' option. Does it exist? the rules were never regenerated after whatever wiped them, and step 7 fixes it. An empty permalink_structure means you are on plain permalinks, where the pretty URL was never valid.

  1. Match the failing path against the rules WordPress actually has.
wp rewrite list --match=/2026/03/my-post/ --fields=match,query,source --format=table

Expected: at least one row whose source is post and whose query contains name=$matches[ — the rule that would resolve your URL. No rows means WordPress has no rule that can ever match this path, which points at a custom post type or a permalink structure that does not cover it, so go to step 10.

  1. Flush the rewrite rules, and flush the object cache with them.
wp rewrite flush --hard
wp cache flush

Expected: Success: Rewrite rules flushed. followed by Success: The cache was flushed. The second command matters more than people expect: with Redis or Memcached in front of the options table, new rules land in the database while every visitor keeps getting the old ones. On Apache, --hard also rewrites the WordPress block in .htaccess, but only if PHP can write to that file — on nginx it has nothing to write and is harmless.

  1. On Apache, verify .htaccess exists, is readable, and is not being ignored.
ls -l .htaccess
grep -A 8 'BEGIN WordPress' .htaccess
apachectl -M 2>/dev/null | grep rewrite

Expected: a file of a few hundred bytes, a block containing RewriteEngine On and RewriteRule . /index.php [L], and rewrite_module (shared) from the last command. A missing block is fixed by step 7 or by pasting the default block back. If the block is correct but nothing changes, Apache is ignoring the file — check the vhost with grep -R AllowOverride /etc/apache2/sites-enabled/ and expect AllowOverride All (or at least FileInfo) on your document root. AllowOverride None means the file is never read; see htaccess overrides and AllowOverride and Apache mod_rewrite basics.

  1. On nginx, verify the request reaches PHP at all.
nginx -T 2>/dev/null | grep -A 4 'location / {'

Expected: a try_files $uri $uri/ /index.php?$args; line inside that block. Without it, nginx looks for a real file and directory, finds neither, and returns its own 404 — the signature from step 3. Add the directive, then:

nginx -t && systemctl reload nginx

Expected: syntax is ok and test is successful before the reload runs. If the test fails, nothing is reloaded and the site keeps running on the old config.

  1. Check custom post types and plugin routes. A CPT registered after init, or whose rewrite or has_archive argument changed, 404s on its own URLs while the rest of the site is fine.
wp eval 'foreach (get_post_types(["_builtin"=>false],"objects") as $t) { printf("%s public=%s has_archive=%s rewrite=%s\n", $t->name, var_export($t->public,true), var_export($t->has_archive,true), json_encode($t->rewrite)); }'

Expected: one line per registered type, for example product public=true has_archive=true rewrite={"slug":"product","with_front":false}. If your type is missing entirely, its registration is not running — register_post_type() has to be called on init, and code firing on wp_loaded or later is too late for rewrite rules to be built from it. If public is false, single URLs 404 by design. Any change to slug or has_archive needs step 7 afterwards. See the register_post_type documentation.

Plugin routes fail the same way for a different reason: they hang off a real page. WooCommerce resolves /my-account/orders/ only because the My Account page exists and orders is registered as an endpoint on it.

wp option get woocommerce_myaccount_page_id
wp post get 42 --field=post_status

Expected: a numeric page ID, and publish for that ID. If that page was trashed, renamed, or lost in a migration, every endpoint under it 404s at once while ordinary posts are untouched. LMS and membership plugins register types and endpoints identically, so the same diagnostic applies.

  1. On multisite, remember rules are stored per site. A network-wide permalink change is not one flush, it is one flush per site.
wp site list --fields=blog_id,url
wp site list --field=url | xargs -I{} wp rewrite flush --url={}

Expected: the site table, then one Success: Rewrite rules flushed. per site. On a subdirectory network the .htaccess block is not the single-site block — it carries extra rules that strip the site path before handing off — so a deploy that dropped in a generic .htaccess breaks every site but the main one.

  1. Purge the edge last, once the origin is right.
curl -sI https://example.com/2026/03/my-post/ | grep -iE 'age|x-cache|cf-cache-status'

Expected after a purge: a MISS and no age header, or an age in the low single digits. Purging before the origin is fixed only reloads the broken response, which is why this step is last.

Validate it works

Do not trust one URL. Sweep every published permalink and read the status codes:

wp post list --post_type=any --post_status=publish --field=url | head -100 | xargs -I{} curl -o /dev/null -s -w '%{http_code} %{url_effective}\n' {}

Expected: a column of 200 values. Any 404 line names the exact URL to take back through step 1, and any 301 deserves a glance — a redirect to a URL that itself 404s is the failure mode people most often miss.

Then confirm the rules survived, which is what separates a real fix from a temporary one:

wp option get rewrite_rules --format=json | head -c 120
curl -sI "https://example.com/2026/03/my-post/?cb=$(date +%s)" | head -1

Expected: a JSON object rather than an error, and HTTP/2 200. Run both again an hour later. Rules that vanish between runs mean something is clearing the option on a schedule, which is the first entry below.

Troubleshooting

The flush works, then the 404s come back on their own. Something is wiping the rules after you: a plugin calling flush_rewrite_rules() on every page load, a security plugin restoring a stored copy of .htaccess, or a deploy shipping .htaccess from a repository where the WordPress block was never committed. Compare wp option get rewrite_rules --format=json | wc -c before and after the site rebreaks — a changed byte count means the option is being rewritten, not the server config.

It works logged in and 404s logged out. Two explanations, easy to tell apart. The post is private or draft, in which case step 4 shows it. Or a page cache is holding an anonymous copy of the earlier 404 while your logged-in requests bypass the cache — repeat step 2, and purge if the cache-busted URL is fine.

Only URLs with (or without) a trailing slash 404. WordPress normally corrects this through redirect_canonical, so a mismatch means a theme or plugin removed that filter, or a proxy rewrite is stripping the slash first. Check both forms with curl -sI: a 404 where you expected a 301 means the canonical handler is not running.

Everything 404s including /wp-admin/. That points below WordPress: either the document root is wrong, or a WAF or ModSecurity rule is blocking the path. Watch the access and error logs while you reload — a request that never appears was stopped before your server, and a 403 with a rule ID is ModSecurity naming the rule that fired.

The homepage 404s but everything else works. Check wp option get show_on_front and wp option get page_on_front. A static front page pointing at a deleted post ID produces precisely this, and it survives every rewrite flush.

When this is the wrong approach

This guide assumes the URLs are supposed to work and something broke. Three cases where it is the wrong tool:

The pages are gone on purpose and the question is what that costs you in search — that is a different job, see WordPress 404 pages and SEO impact.

The site was migrated and the URL structure genuinely changed. No rewrite surgery resurrects a path that no longer describes anything; you need a redirect map from the old structure to the new one, served at the server or CDN rather than by a plugin on every request.

The front end is decoupled. If WordPress only serves the REST API and a separate application renders the pages, a 404 in the browser is that application’s routing and the rewrite layer is not involved. Test the REST endpoint directly first. WP-CLI’s rewrite command reference is worth a read either way, since these are the subcommands to script into a post-deploy check.

Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.