WordPress Problem Solving

WordPress Posts Returning 404? Fix Permalinks the Right Way

Posts that return 404 while the home page works are a rewrite problem, not lost content. How to tell a server fault from a WordPress one, then fix rewrite rules, .htaccess, nginx, custom post types and slug clashes.

WordPress Posts Returning 404? Fix Permalinks the Right Way

When WordPress posts 404 but the home page still loads, the content is almost never gone. The posts are in the database. What has broken is the step that turns a readable address such as /blog/my-post/ into the internal query WordPress understands. That step depends on a set of rewrite rules stored by WordPress, and on a web server that hands unknown addresses to index.php.

Either half can fail, and each has its own fix. One short test tells you which half you are dealing with.

Two kinds of 404 and what each one means

Look at the error page itself, then try the post's plain address. Every post has one that needs no rewriting: https://example.com/?p=123, where the number is the ID shown as post=123 in the address bar of the edit screen.

What you seeWhere the fault is
A bare "Not Found" page from Apache or nginx, with none of your designThe web server. The request never reached WordPress.
Your theme's own 404 page, with header and footerWordPress. It received the request and matched it to nothing.
The plain ?p=123 address works and the pretty one does notRewriting, on one side or the other. The content is safe.
The plain address fails tooThe post itself: it is a draft, private, scheduled or in the trash.

Flush the rewrite rules first

WordPress compiles its rules into one database option and rebuilds them only when told to. Telling it is the first fix to try, because it is quick and touches no content.

  1. Go to Settings > Permalinks.
  2. Change nothing.
  3. Press Save Changes.

That rebuilds the rules, and on Apache it also rewrites the WordPress block in .htaccess when the file is writable. From the command line, wp rewrite flush rebuilds the stored rules.

If the posts come back, ask why the rules were stale. The usual reasons are a plugin or theme that added a content type without flushing, and a migration. If the 404s return a few days later, some code is rebuilding the rules at the wrong moment, and the custom post type section below applies.

Fix the web server side

When the 404 comes from the server and not from your theme, the fix depends on which web server runs the site. Your host's documentation or the Server response header will tell you.

Apache: .htaccess, mod_rewrite and AllowOverride

A server-level 404 on Apache has four possible causes. Download a copy of .htaccess before you edit it, then check them in this order.

  • The file is missing. .htaccess is hidden, and migrations lose it because some tools skip files that start with a dot. It belongs in the site's root folder, next to index.php.
  • WordPress cannot write it. In that case the Permalinks screen prints the rules at the bottom of the page after you save, for you to paste into the file by hand.
  • The rewrite module is off. Shared hosts enable it as standard. On your own Debian or Ubuntu server, run sudo a2enmod rewrite and restart Apache.
  • Apache is told to ignore the file. A fresh virtual host often carries AllowOverride None, which disables .htaccess altogether. This is the classic cause on a new VPS.

The last one is fixed in the site's virtual host configuration, followed by a reload of Apache:

<Directory /var/www/example.com/public_html>
    AllowOverride All
</Directory>

For a site installed in a subfolder, the RewriteBase line and the final rule must include that folder. Saving the Permalinks screen writes the correct values, so do that and do not edit them by hand.

Nginx: the try_files line

Nginx ignores .htaccess completely. The equivalent lives in the site's server block, and it is one directive:

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

Without it, nginx looks for a real file or folder at /my-post/, finds none and returns its own 404. After adding it, test and reload:

sudo nginx -t
sudo systemctl reload nginx

The handbook has a full nginx configuration for WordPress. On managed hosting you cannot edit this file, so send the host the line and ask them to confirm it is in place. Keep in mind that a site moved from Apache to nginx also loses every redirect that used to live in .htaccess.

When only some WordPress posts 404, look at custom post types

Standard posts and pages work, and everything under "Projects", "Events" or another custom type returns the theme's 404 page. The rules for that type are not in the stored set, because the set was built before the type was registered.

Saving the Permalinks screen fixes it for today. The lasting fix is in the code that registers the type. It should flush the rules once, on activation, after the type exists:

function mysite_register_types() {
    register_post_type( 'project', array(
        'label'       => 'Projects',
        'public'      => true,
        'has_archive' => true,
        'rewrite'     => array( 'slug' => 'projects' ),
    ) );
}
add_action( 'init', 'mysite_register_types' );

register_activation_hook( __FILE__, function () {
    mysite_register_types();
    flush_rewrite_rules();
} );

Three related mistakes cause the same symptom:

  • Flushing on every request. Calling flush_rewrite_rules() inside an init callback slows every page and, under load, produces 404s that come and go. The function reference says plainly that it is an expensive operation.
  • Arguments that switch URLs off. A type needs 'public' => true (or 'publicly_queryable' => true) to have single pages, and 'has_archive' => true to have a listing at /projects/.
  • Registering the type in the theme. Switch themes and the type disappears, along with every address under it. Content types belong in a plugin, which is routine plugin development work.

Slugs that fight each other

WordPress tests an address against its rules from the top, and the first match wins. When two things claim one path, one of them returns a 404 or the wrong content.

The common clash is a page whose slug equals a post type's archive or rewrite slug. With a page called "projects" and a type that uses /projects/, the type wins, and a child page at /projects/about/ is read as a project named "about" that does not exist. The same happens when a page slug matches the category base or the tag base. Rename one side.

Unexpected suffixes are a related clue. Slugs are unique within their group, and media files take slugs too. Upload contact.jpg first, and a top-level page created later may be saved as contact-2 while your menu still points at /contact/.

To see which rule claims an address, ask WP-CLI:

wp rewrite list --match=/projects/about/

It prints the rules that match that address, each with the query it produces, so you can see what WordPress thinks the address means. The wp rewrite list reference describes the output.

What not to do, and when to get help

Do not change the permalink structure of an established site to make the 404s stop. That changes every address at once, and without redirects it throws away search traffic and inbound links. The planning that needs is covered in our website migration SEO checklist. Do not delete and recreate posts either. The originals are fine.

The Permalinks screen and a check of .htaccess are safe for anyone to do. Server configuration on hosting you do not control belongs to the host. A developer is worth calling when the 404s followed a move between hosts and several causes overlap, when custom code adds its own rewrite rules, or when a store or course plugin and your pages compete for the same base slugs. Our WordPress maintenance service handles all three.

Frequently asked questions

No, as long as you leave the selected structure as it is. Saving without changes only rebuilds the stored rules and, on Apache, the WordPress block in .htaccess.

Why does the home page work when every other page returns 404?

The home page is served by index.php at the root of the site, which the web server finds without any rewriting. Every other pretty address exists only as a rule, so it fails as soon as rewriting does.

Why did posts start returning 404 after I moved hosts?

Usually because .htaccess was not copied, the new Apache server ignores it, or the new server runs nginx and has no try_files line for the site. Save the Permalinks screen first, then check the server configuration.

That is normally a theme template running its own query with a different number of posts per page than the main query. WordPress decides whether page 2 exists from the main query. The fix is to adjust the main query with the pre_get_posts hook and stop replacing it in the template.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

Swati Priyadrshni
· Founder & CEO at WorldWin Coder

I'm Swati Priyadarshani, Founder & CEO of LMS Advisor and Co-Founder of WorldWin Coder Pvt. Ltd. (est. 2019). Over the last 10+ years, I've helped enterprises build learning platforms that actually get used - not just deployed.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.