Fix 404 errors in WordPress
A 404 error means the server was reached but could not find anything at that address. In WordPress, it shows up in two quite different ways: every post and page except the home page returns 404, or just a few addresses do. This guide helps you tell which one you have and fix it. It is for site owners who can use the dashboard and, for some steps, connect with SFTP.
Which problem do you have?
| What you see | Likely cause | Go to |
|---|---|---|
| The home page works, but every post and page returns 404 | The rewrite rules are missing or not working | Resave the permalinks |
| Only certain addresses return 404 | The content was deleted or its address changed | Missing or moved pages |
| Images or files return 404 | The files are missing or in the wrong folder | Check the file location with SFTP |
How WordPress addresses work
WordPress pages do not exist as separate files on the server. A permalink such as https://example.com/opening-hours/ is only a readable address. Rewrite rules on the web server pass every such request to WordPress's index.php, which then looks up the right content. If those rules are missing, the server looks for a real folder called opening-hours, finds nothing, and returns 404.
Resave the permalinks
This is the quickest fix and often enough.
- Sign in to the dashboard and open Settings > Permalinks.
- Without changing anything, choose Save Changes.
- Open one of the broken posts again in a private browser window.
Saving tells WordPress to rebuild its rewrite rules and, on Apache servers, to write them to the .htaccess file if it can.
Check the .htaccess file
On servers that run Apache or a compatible web server, the rewrite rules live in a file called .htaccess in the site's main folder, next to wp-config.php. Its name starts with a dot, so your SFTP client may hide it; turn on "show hidden files". See Redirects and .htaccess.
-
Connect with SFTP and download a copy of
.htaccess, if it exists, as a backup. -
Open it in a plain text editor. If it is missing, create a new file called
.htaccess. -
Make sure it contains the standard WordPress block:
# BEGIN WordPress<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPress -
Save the file, upload it, and test the site again.
If WordPress is installed in a subfolder, such as https://example.com/blog/, change RewriteBase / to RewriteBase /blog/ and the last rule to /blog/index.php.
Keep any other rules in the file, such as redirects or security rules, but put them outside the # BEGIN WordPress and # END WordPress lines. WordPress may rewrite everything between those lines.
A typing error in .htaccess usually causes a "500 Internal Server Error" on every page. If that happens, upload your backup copy again.
mod_rewrite
The block above relies on an Apache module called mod_rewrite. Almost every host that supports WordPress has it turned on. If the rules are correct and pages still return 404, ask your host to confirm that mod_rewrite is enabled and that .htaccess files are allowed to change rewrite settings.
Sites on nginx
The nginx web server does not read .htaccess files. Its rewrite rules live in the server configuration, which you can change on a VPS but usually not on shared hosting. A typical rule for WordPress, inside the site's server block, is:
location / {
try_files $uri $uri/ /index.php?$args;
}
It tells nginx to serve a real file or folder if one exists and otherwise to pass the request to WordPress. Reload nginx after changing its configuration. On shared hosting, contact your host instead.
Missing or moved pages
If only some addresses return 404, the rewrite rules are working and the content is the problem.
- Deleted content. Check Posts and Pages, including the Trash view. Restore the item if it was deleted by mistake.
- A changed address. Changing a post's slug (the last part of its address), its parent page or the permalink structure changes its address. Old links, bookmarks and search results still point to the old one.
- Old links. Search engines and other sites may still link to addresses from a previous version of the site.
For content that has moved, add a 301 redirect from the old address to the new one. A 301 tells browsers and search engines that the move is permanent. You can add redirects with a redirect plugin or as rules in .htaccess. See Redirects and .htaccess.
For content that is gone for good, a 404 is the correct answer. Make sure your theme's 404 page helps visitors, for example with a search box and a link to the home page.