Home » .htaccess » WordPress Posts 404 but Homepage Works: Fix Permalinks

WordPress Posts 404 but Homepage Works: Fix Permalinks

The homepage loads fine, wp-admin works, but every post and page returns a 404 Not Found. Sometimes it is the server’s plain “Not Found” page, sometimes your theme’s 404 template.

This almost always happens right after a migration, a server or web server change, a restore from backup, or after someone touched .htaccess. The content is still in the database. WordPress just never gets the request for /my-post/, or it gets it and has no rewrite rule to match it.

Work through the steps in order. Most sites are fixed by Step 2 or Step 3.


Open a broken post and look at the page. This tells you where the request dies.

  • A bare white “Not Found” page from Apache or nginx means the web server never passed the request to index.php. Go to Step 3 and Step 4 (Apache) or Step 5 (nginx).
  • Your theme’s styled 404 page means WordPress got the request but its rewrite rules did not match. Step 2 usually fixes it.

A quick test: add ?p=1 to your homepage URL (or use the ID of any published post). If https://example.com/?p=1 opens the post while the pretty URL gives a 404, the content is fine and the problem is only the permalink rewriting.

WordPress caches its rewrite rules in the rewrite_rules option in the database. After a migration, or when a plugin that registers custom post types misbehaves, that cache can be stale or empty.

The easy way: go to Settings → Permalinks and click Save Changes without changing anything. The Permalinks screen documentation confirms that just visiting that screen triggers a flush of rewrite rules.

With SSH access, do it with WP-CLI from the WordPress root:

wp rewrite flush

You should see Success: Rewrite rules flushed. Then reload a post in a private window.

To also rewrite the .htaccess file, use the --hard flag. According to the wp rewrite flush docs, it works only on single site installs, and WP-CLI needs to know Apache has mod_rewrite. Back up .htaccess, then add this to wp-cli.yml in the WordPress root:

apache_modules:
  - mod_rewrite
wp rewrite flush --hard

If the 404 is gone, you are done. If you still get the bare server “Not Found” page, flushing can’t help, because the request never reaches WordPress. Keep going.


On Apache and LiteSpeed, pretty permalinks depend on a small block of rewrite rules in the .htaccess file in the WordPress root. Migration tools and FTP clients often skip it because it is a hidden dotfile, so the new server simply doesn’t have it.

Back up the current .htaccess if one exists, then make sure it contains the default block. This is the “Basic WP” block from the official Apache HTTPD / .htaccess guide:

# 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

If WordPress lives in a subfolder like /blog/, change RewriteBase / to RewriteBase /blog/ and the last line to RewriteRule . /blog/index.php [L].

💡 Can’t see the file? Turn on “show hidden files” in your file manager or FTP client. I covered that in how to find and create the .htaccess file.

If .htaccess is not readable by the web server, you will usually get a 403 instead of a 404. That case is in 403 Forbidden error in WordPress.

A perfect .htaccess does nothing if Apache ignores it. This is the usual cause on a fresh VPS, where the vhost has AllowOverride None or mod_rewrite is not enabled.

See also  403 Forbidden Error in WordPress: Find the Cause and Fix It

A fast way to test: put a line of garbage like THIS_IS_A_TEST at the top of .htaccess and reload the site. If you get a 500 Internal Server Error, Apache is reading the file (remove the line right away). If the site loads normally, the file is being ignored.

On Debian and Ubuntu, enable the module:

sudo a2enmod rewrite

Then allow overrides for the document root in your vhost (for example /etc/apache2/sites-available/example.com.conf). Back up the file and adjust the path to your site:

<Directory /var/www/example.com/public_html>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Test the config and reload Apache:

sudo apachectl configtest
sudo systemctl reload apache2

configtest should print Syntax OK. If it prints an error, fix the line it names before reloading. On RHEL-based systems the service is httpd instead of apache2.


nginx ignores .htaccess completely. The official nginx guide points out that WordPress cannot generate the rewrite rules for you there, so a site moved from Apache to nginx will 404 on every post until the server block has this:

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

For a subfolder install, use location /blog/ and /blog/index.php?$args. Then test and reload:

sudo nginx -t
sudo systemctl reload nginx

nginx -t should report that the syntax is ok and the test is successful. Don’t reload if it reports an error.

In OpenPanel, you open a domain’s config from the Domains page with Edit VHosts, and OpenPanel runs a syntax check on save and rolls back if it fails. On cPanel you normally only get .htaccess. OpenPanel accounts can run Apache, Nginx, OpenLiteSpeed, LiteSpeed or OpenResty, so check which one yours uses before you choose between Step 3 and Step 5.


If most posts load and only a few return 404, the server is fine. Look at what those URLs have in common.

  • A page and a category or custom post type share the same slug, and the wrong rewrite rule wins.
  • A plugin that registered a custom post type was deactivated, so its posts have no rewrite rules anymore.
  • A redirect or security plugin is catching the URL before WordPress does.

WP-CLI shows which rewrite rules match a URL, which saves a lot of guessing:

wp rewrite list --match=/my-broken-post/

If nothing comes back, no rule in WordPress claims that URL. If the first match is a rule for a different post type or taxonomy, rename one of the conflicting slugs and flush again.

💡 After activating or deactivating any plugin that adds post types, flush the rewrite rules once (Step 2).


To keep this from coming back after the next move, copy .htaccess together with the files and check that it exists on the new server. When you switch to nginx, add the try_files line before you change DNS. My WordPress .htaccess file examples post has more rules you can place around the WordPress block.

TL;DR: If WordPress posts return a 404 while the homepage works, save Settings → Permalinks or run wp rewrite flush. If that doesn’t help, restore the WordPress block in .htaccess and make sure Apache has mod_rewrite and AllowOverride All. On nginx, add try_files $uri $uri/ /index.php?$args; to the server block.

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended