WordPress Posts Return 404 but the Homepage Works
Fix WordPress posts that 404 while the homepage loads: re-save permalinks, confirm .htaccess is writable, and check AllowOverride and mod_rewrite.
If your homepage loads fine but every post and page returns a 404, the URL rewrite rules WordPress relies on are missing, broken or ignored. The fastest fix: open Settings → Permalinks in wp-admin. Loading that screen makes WordPress rebuild its rewrite rules and, when it can write the file, the block in .htaccess. Clicking Save Changes without editing anything does the same again. If the 404s stay, one of four things is true: WordPress can’t write .htaccess, Apache is set to ignore it, mod_rewrite isn’t running, or you’re on nginx, which doesn’t use .htaccess at all.
Why the homepage works but posts don’t
Your homepage works because a request to / is served straight from index.php at the document root. No rewriting is needed.
A request to /2026/my-post/ is different. That path is not a real folder on disk. For WordPress to answer it, Apache has to route the request internally to index.php. WordPress then reads REQUEST_URI, matches it against the rewrite rules stored in the rewrite_rules option in the database, resolves the query vars (WP::parse_request()), and loads the right post.
The handoff from Apache to index.php is the part that breaks. It depends on this block in .htaccess, which is what WordPress 7.1.4 writes for a site at the web root (read from WP_Rewrite::mod_rewrite_rules() in the source):
# 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
The two RewriteCond lines say: if the requested path is not a real file (!-f) and not a real directory (!-d), send it to index.php. The HTTP_AUTHORIZATION line passes the login header to PHP and has nothing to do with the 404. Our default WordPress .htaccess file guide explains every line.
Without the block, Apache looks for a literal /2026/my-post/ directory, doesn’t find one, and returns its own 404 before WordPress sees the request. We checked this on 2026-11-01 with Apache 2.4.58 and PHP 8.3.6, using a one-line index.php in place of WordPress. With no .htaccess, / returned 200 and /2026/10/post-name/ returned 404. With the default block in place, both returned 200.
When WordPress rewrites .htaccess, and when it can’t
This is the part most guides skip. The Permalinks screen calls flush_rewrite_rules() every time it loads, not only when you click Save. That rebuilds the rules in the database and then calls save_mod_rewrite_rules(), which writes the block with insert_with_markers().
Reading the WordPress 7.1.4 source (not run), save_mod_rewrite_rules() only writes the file when all of these hold:
- The site is not Multisite. WordPress never writes
.htaccesson a network. - WordPress thinks the server is Apache with
mod_rewrite. It treats LiteSpeed as Apache. - The file is writable, or it doesn’t exist yet, its folder is writable and pretty permalinks are on.
insert_with_markers() only touches the lines between # BEGIN WordPress and # END WordPress. Everything inside is discarded and rewritten; everything outside is left alone. WordPress also writes a warning into the file:
Any changes to the directives between these markers will be overwritten.
If WordPress can’t write the file, the Permalinks screen tells you. It shows “Error: Your .htaccess file is not writable, so updating it automatically was not possible”, followed by the rules in a read-only box. After you save a changed structure, the notice at the top reads “You should update your .htaccess file now.” Both messages appear only when the rules in the file differ from the ones WordPress wants. On nginx, WordPress doesn’t try to write anything and shows neither message.
How to fix it, in order
1. Open the Permalinks screen. Go to Settings → Permalinks. Loading the page is enough; Save Changes with no edits does no harm. If you work from the command line, wp rewrite flush --hard does the same job. WP-CLI’s docs say the hard flush works only on single sites, and that WP-CLI can only write .htaccess if you add this to wp-cli.yml:
apache_modules:
- mod_rewrite

2. Check that .htaccess exists and is writable. It lives in the same folder as wp-config.php. It’s a dotfile, so enable “show hidden files” in your FTP client or use:
ls -la /path/to/wordpress/.htaccess
If it’s missing, create it and paste the block from the Permalinks screen, or the one above. If your install lives in a subfolder, RewriteBase and the last rule must name that folder, for example RewriteBase /blog/ and RewriteRule . /blog/index.php [L]. In our 2026-11-16 Apache test, the root block left unchanged in /blog/.htaccess sent /blog/2026/10/post-name/ to the index.php at the web root instead of /blog/index.php. Our .htaccess generator writes the root version (RewriteBase / and /index.php), so change those two lines by hand for a subfolder install.
3. Confirm Apache is allowed to run .htaccess rules. With AllowOverride None, Apache ignores the file completely, whatever it contains. Rewrite rules need at least AllowOverride FileInfo. Rewrite rules in .htaccess also need Options FollowSymLinks or SymLinksIfOwnerMatch. That is Apache’s default, but some hosts turn it off. Both settings live in the server config, so they need server access or your host:
<Directory /var/www/html>
Options FollowSymLinks
AllowOverride All
</Directory>
AllowOverride All is the safer choice if you also add hardening rules. With FileInfo alone, the WordPress block works but common Options and Require lines return a 500.
To find out whether Apache reads the file at all, use the check from Apache’s own docs. Put one line of nonsense at the top of .htaccess and load any page. A 500 error means the file is being read. A normal page means it’s ignored. Remove the line straight away. In our 2026-11-16 test (Apache 2.4.58), the line ThisIsNotADirective gave a normal 200 under AllowOverride None and a 500 under AllowOverride All. The error log named it: “Invalid command ‘ThisIsNotADirective’”.
4. Confirm mod_rewrite is enabled. The block sits inside <IfModule mod_rewrite.c>, so without the module Apache skips it silently and posts 404. On Debian/Ubuntu:
sudo a2enmod rewrite
sudo systemctl restart apache2
5. On nginx, there is no .htaccess. Rewrites go in the server block instead:
location / {
try_files $uri $uri/ /index.php?$args;
}
Quick diagnostic: whose 404 is it?
Look at the 404 page. If it’s an unstyled Apache or host error page, Apache never reached WordPress. That’s a .htaccess or server-config problem (steps 2 to 4). If it’s your theme’s styled 404, WordPress received the request but couldn’t match it. The rules in the database are stale, and opening the Permalinks screen (step 1) is the fix.
| Symptom | What’s actually happening | Fix |
|---|---|---|
| Plain, unstyled 404 (“Not Found” / Apache’s own error page, no theme styling) | Apache looked for a literal file or folder at that path, found nothing, and never handed the request to index.php | Rebuild .htaccess (step 2), check AllowOverride (step 3), confirm mod_rewrite is enabled (step 4) |
| Your theme’s styled 404 page | The request reached WordPress, but WP::parse_request() couldn’t match it against the stored rewrite rules | Open Settings → Permalinks (step 1) to flush rewrite_rules |
| Normal posts work, but a custom post type, shop or portfolio URL 404s | The post type was registered after the rules were last built. WordPress’s docs note new custom post types usually need a manual flush | Open Settings → Permalinks (step 1) |
| Every URL, homepage included, returns 403 Forbidden | FollowSymLinks and SymLinksIfOwnerMatch are both off. In our test the log read “AH00670: Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive is also forbidden” | Turn one of them on for the directory (step 3) |
| Every URL returns 500 right after an edit | A line in .htaccess isn’t allowed or doesn’t exist on this server | Undo the edit and read the error log; the hardening guide lists the usual causes |
| 404 only on posts/pages, homepage fine, on nginx | There’s no .htaccess to be missing. nginx never reads it | Add the try_files rule to the server block (step 5) |
404s continue after opening the Permalinks screen, .htaccess timestamp unchanged | WordPress couldn’t write the file, so the new block never reached disk | Fix file ownership or permissions, or copy the rules the Permalinks screen shows into the file, replacing the old WordPress block |
| 404 comes back minutes or hours after a fix | A security plugin or host panel is regenerating or locking .htaccess behind you | Check the file’s modification time right after a save (see “Still stuck?” below) |
Apache vs. nginx: what each one needs
Apache and nginx solve the same problem, “route anything that isn’t a real file to index.php”, but the configuration lives in different places and uses different syntax:
| Apache | nginx | |
|---|---|---|
| Where the rewrite lives | .htaccess in the WordPress root (or the vhost config) | The server block only. There is no per-directory override file |
| Who can edit it | Anyone with FTP/file access, if AllowOverride permits | Only someone with access to the nginx config and permission to reload the service |
| What WordPress can do to it | flush_rewrite_rules() writes the # BEGIN WordPress block through insert_with_markers() | Nothing. WordPress can’t change nginx config; you maintain it by hand |
| Directive that does the routing | RewriteRule . /index.php [L] | try_files $uri $uri/ /index.php?$args; |
| Reload needed after a change | No. Apache reads .htaccess on every request | Yes: nginx -s reload or a service restart |
That last row is the one people on nginx usually trip over: editing the server block and expecting it to take effect without a reload.
What not to do
Don’t start by deactivating all your plugins. A plain “posts 404, homepage works” symptom is a rewrite problem first. Work through the steps above before you test plugins, unless only URLs that a plugin adds are failing.
Don’t chmod 777 your .htaccess. WordPress needs the file writable by the web server user, which is not the same thing as world-writable. 644 with the correct owner is fine and safe; 777 is a security hole that won’t fix the routing anyway.
Don’t paste custom rewrite rules inside the # BEGIN WordPress / # END WordPress markers. They’ll be wiped the next time anyone opens the Permalinks screen. Put custom directives above the # BEGIN WordPress line. The same rule applies if you’re adding redirect rules or hardening directives to the same file.
Don’t reinstall WordPress. The core files are fine. This is one missing block of text, not a corrupt install.
Don’t expect a theme switch to fix it. Permalink routing is WordPress core. A theme only matters when it registers its own post types, and those 404s clear when you flush the rules in step 1.
Still stuck?
If you’ve opened the Permalinks screen, confirmed the block is present and writable, and checked AllowOverride, the remaining suspect is a security plugin or a host-level rule regenerating or locking .htaccess behind your back. Code can also stop the file write on purpose through the flush_rewrite_rules_hard filter; the database rules still update, but the file doesn’t. Check the file’s modification time right after opening the Permalinks screen. If it didn’t change, something is blocking the write. Build a clean block with the .htaccess generator, paste it in, and re-test with a hard refresh.
If this started right after a domain or http → https migration, check whether the 404s are a side effect of the move rather than the rewrite rules. Our mixed content after moving to HTTPS guide covers the other problem that shows up at the same time.
Sources
- WordPress Advanced Administration: .htaccess, Nginx
- WordPress Developer Reference: flush_rewrite_rules()
- WP-CLI: wp rewrite flush
- WordPress source code: class-wp-rewrite.php, options-permalink.php, misc.php, vars.php
- Apache HTTP Server 2.4: .htaccess files, AllowOverride and Options (core), mod_rewrite
William Lopez is the founder of Odie Themes. He builds the free WordPress tools and themes on this site and has been on WordPress.org since December 2019.
FAQ
Questions
Why do my WordPress posts return 404 but the homepage works?
The URL rewrite rules WordPress relies on are missing, broken or ignored. The homepage works because a request to / is served straight from index.php at the document root. A post URL is not a real folder on disk, so Apache needs the rewrite block in .htaccess to hand the request to index.php.
How do I fix WordPress 404 errors on all pages?
Open Settings, then Permalinks, in wp-admin. Loading that screen makes WordPress rebuild its rewrite rules and, if it can write the file, the block in .htaccess. Clicking Save Changes without editing anything does the same. If WordPress can't write the file, the screen shows the rules for you to paste in.
Where is the .htaccess file in WordPress?
It sits in the same folder as wp-config.php, at the root of your WordPress install. It is a dotfile, so it stays hidden until you turn on show hidden files in your FTP client, or list it directly with ls -la on the path to your install.
Why does WordPress keep overwriting my .htaccess file?
WordPress only rewrites the lines between the # BEGIN WordPress and # END WordPress markers, and only when it can write the file. Everything outside those markers is left alone, so put custom directives above the # BEGIN WordPress line to stop them being wiped.
Why are my permalinks still 404 after re-saving them?
Apache is probably not running the rules. With AllowOverride None it skips .htaccess completely, so the directory needs AllowOverride FileInfo or All. Rewrite rules in .htaccess also need Options FollowSymLinks or SymLinksIfOwnerMatch, and mod_rewrite must be loaded. nginx does not read .htaccess at all.