Skip to content
ThemesIonic — home
Troubleshooting

WordPress Permalinks Not Working: Rewrite Rules, .htaccess and Nginx

A pretty URL passes through the web server's rewrite, WordPress's stored rules and the slug lookup. Which 404 you get tells you which layer failed, and only one of them is fixed from the admin.

Updated 6 min read intermediate

Quick fix: open Settings → Permalinks. Loading that screen rebuilds WordPress's rewrite rules, and on Apache it also rewrites the WordPress block in .htaccess if the file is writable. If pretty URLs still 404 after that, the web server is not handing them to WordPress, and nothing else in the admin will change it.

Do not switch to Plain permalinks to make the errors go away. That changes every URL on the site and turns a server problem into an SEO one.

Three layers, three kinds of failure

A request for /my-post/ passes through:

  1. The server rewrite. Apache and LiteSpeed read the WordPress block in .htaccess; Nginx needs try_files in its config. Either way, a path that is not a real file goes to index.php. If this fails, WordPress never runs.
  2. WordPress's rewrite rules. A list of patterns stored in the rewrite_rules option. When the option is empty, WordPress rebuilds it on the next request. When it exists but predates a newly registered post type or taxonomy, the new URLs match nothing.
  3. The content lookup. The matched slug must belong to exactly one published object, or WordPress returns its 404 template — or guesses and redirects.

Which layer: symptom → check → fix

Symptom Layer Check Fix
Bare "Not Found" page from Apache, Nginx or LiteSpeed, no theme Server rewrite Response has no Link: <…/wp-json/>; rel="https://api.w.org/" header Restore the .htaccess block or try_files
Theme's 404 on every pretty URL, homepage works WordPress rules or structure Compare the URL with the structure in Settings → Permalinks Open Settings → Permalinks; restore the old structure if it changed
Theme's 404 only on a custom post type or new taxonomy Stale rules wp rewrite list shows no rule for that slug Open Settings → Permalinks once
Theme's 404 on one URL Content Post status, slug, or a page with the same slug Fix the slug or status
URL redirects to a different post with a similar name Content The address you typed matches no post exactly Fix the link; WordPress guessed by slug prefix
Every option on the Permalinks screen starts with /index.php/ Server detection WordPress decided the server cannot rewrite See the /index.php/ section below
500 error right after editing .htaccess Server rewrite Server error log Fix the syntax — see 500 Internal Server Error

The header check works because WordPress adds that Link header to every front-end response it generates, its own 404 pages included. Confirm the homepage sends it first, since some security plugins strip it; then a 404 without it came from the server:

curl -sI "https://example.com/?p=123" | grep -iE "^HTTP|^location"
curl -sI "https://example.com/my-post/" | grep -iE "^HTTP|^server|^link"

The first request needs no rewriting, so WordPress answers it and normally 301-redirects to the pretty URL. That redirect proves WordPress and the database are fine; if the second request then 404s without the Link header, the fault is purely in the server layer. If the homepage fails too, it is not a permalink problem — see WordPress site not loading.

Apache and LiteSpeed: when WordPress writes .htaccess

On every hard flush — loading or saving the Permalinks screen, or flush_rewrite_rules() — WordPress calls save_mod_rewrite_rules(), which writes between # BEGIN WordPress and # END WordPress only if all of these hold:

  • the site is not multisite (multisite never writes this file);
  • .htaccess is writable, or does not exist and the directory is writable;
  • got_mod_rewrite() returns true, meaning WordPress detected Apache or LiteSpeed with mod_rewrite.

The block it writes for a root install, after three comment lines warning that edits between the markers will be overwritten:

<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>

When the file is not writable and its block differs from what WordPress would write, saving shows "You should update your .htaccess file now." and the screen shows: "Error: Your .htaccess file is not writable, so updating it automatically was not possible. These are the mod_rewrite rules you should have in your .htaccess file. Click in the field and press Ctrl + A (or ⌘ + A on Mac) to select all." Paste the Rewrite rules box between the markers by hand.

On a WordPress 7.1.5 test install with the Post name structure, the two checks look like this:

$ curl -sI 'http://localhost:5179/?p=1'
HTTP/1.1 301 Moved Permanently
Location: http://localhost:5179/hello-world/

$ curl -sI 'http://localhost:5179/no-such-post/'
HTTP/1.1 404 Not Found
Link: <http://localhost:5179/wp-json/>; rel="https://api.w.org/"

The 404 carries the Link header, so WordPress produced it; a server-generated 404 would not have it.

If the block is correct and pretty URLs still fail, look outside it: a rule above it that catches everything with [L], a subdirectory install whose RewriteBase and last rule lack the directory, a host that ignores .htaccess for that directory, or mod_rewrite not loaded. What belongs in the rest of the file is covered in the default WordPress .htaccess file.

Nginx and Caddy

WordPress recognises both from the server software string and never tries to write their configuration. Saving permalinks therefore shows a plain "Permalink structure updated." with no warning, even when the server cannot route a single pretty URL. On Nginx the site needs:

location / {
    try_files $uri $uri/ /index.php?$args;
}

For a site in /blog/, use location /blog/ and /blog/index.php?$args. The change requires a reload; on managed hosting, ask support. This is the usual culprit when permalinks broke right after moving to a new host.

The /index.php/ fallback

If got_url_rewrite() returns false — the server was not identified as Apache or LiteSpeed with mod_rewrite, Nginx, Caddy or IIS with URL Rewrite — the Permalinks screen offers every structure with an /index.php/ prefix. These "PATHINFO" permalinks work without any rewrite configuration because each request names index.php directly.

Seeing that prefix means detection failed, not that permalinks broke. Fix the server first. Moving from /index.php/my-post/ to /my-post/ changes every URL, so serve one direct 301 per old address; a rule that redirects back onto itself shows up as ERR_TOO_MANY_REDIRECTS.

Custom post types returning 404

A plugin registers its post type on every request, but the stored rules gain its patterns only when they are rebuilt. Open Settings → Permalinks once after activating it. If you register the type in your own code, flush on activation only — flushing on every page load rebuilds and saves the whole rule set on every request.

Then check for collisions: a Page called "Events" and a post type with the slug events both claim /events/, and one of them becomes unreachable.

Slugs that keep gaining -2

WordPress appends -2 when the slug is taken by something it checks. The rules are narrower than often claimed:

  • Posts must be unique within their own post type. A post and a page can share a slug; with /%postname%/ the page wins /slug/ and the post becomes unreachable.
  • Pages also collide with media at the same level, so an image uploaded as pricing.jpg without being attached to anything blocks a top-level page called "pricing".
  • Drafts and pending posts usually hold no slug yet (it is assigned on publish), so they rarely cause it; scheduled and private posts do.
  • Trashed posts no longer block a slug: since WordPress 5.4 the trashed one is renamed with a __trashed suffix.
  • embed, feed names such as feed or rss, and numeric slugs that could be read as a date or a page number always get a suffix.

Verify

With caches purged, a post, a page, a category archive, /page/2/ and a custom post type single should all return 200, and a made-up URL should return the theme's 404 with the WordPress Link header.

Frequently asked

Only if you select a different structure. Simply opening Settings → Permalinks already rebuilds the rewrite rules and, where the file is writable, the WordPress block in .htaccess. Clicking Save Changes with the same option selected does the same and leaves every URL as it was.
The block editor shows the Slug row only for post types that are publicly viewable and have a link. The slug is editable only when the permalink structure contains %postname% (or %pagename% for pages), so with Plain permalinks there is nothing to edit. Switch to a structure such as Post name, then add redirects if the site already has traffic.
Yes. Nginx ignores .htaccess and WordPress never writes Nginx configuration. The server block needs try_files $uri $uri/ /index.php?$args; and saving permalinks shows "Permalink structure updated." whether that line exists or not.

Related guides