How to Fix 404 Error and Flush Rewrite Rules in WordPress?

Today, we will teach you how to resolve the most prevalent permalink problem, which is a 404 WordPress error that appears when a page is accessed. In most situations, the problem is caused by incorrect rewrite rule settings, and all that has to be done is to flush rewrite rules.

This usually happens when a new WordPress website or a third-party plugin that uses custom post types is installed. There is no reason to worry if this is the case. If you have full control over your WordPress installation and its backend, fixing this issue should be a piece of cake. A WordPress administrator may usually remedy this problem by logging into the WordPress dashboard, however this is not always possible.

We will not go through the terminology in this article since we want it to be as easy to comprehend as possible. If you are new to WordPress, you may learn more about what is a permalink in a different post.

Before we get into the meat of the issue, let us go over how WordPress URLs appear by default. When you open the permalink settings page, you will have seven options to choose from. We will not go through each one in depth, but it is worth noting that the first one, “Plain” should be pre-selected when WordPress is installed.

  • Plain
  • Day and name
  • Month and name
  • Numeric
  • Post name
  • Custom structure

Default permalink settings

However, because this permalink structure is not SEO friendly, you must pick any other option other than the first one to allow pretty permalinks. The .htaccess file should be generated and the rewrite rules should be flushed automatically as a result of this.

Example of a “plain” default permalink

http://example.com/?p=1256

Example of a “pretty” SEO-friendly permalink

http://example.com/blog/lorem-ipsum-dolor/

How to Fix WordPress 404 Page Not Found Error?

Now, let us get to the point and explain what you should do if you see a “404 not found” error when you attempt to access any page on your website even after you have enabled “pretty permalinks” on it. As long as you have access to the admin panel and can visit the homepage, resolving the WordPress 404 error should take no more than a few minutes.

If you attempt to visit one of the subpages, one of the following screens will be displayed, depending on your server type. This is not the only option, as the default WordPress 404 error page may appear entirely different depending on your server settings.

Apache WordPress 404 error
If your server is running Apache, you will see this page as the default error page.
404 page on NGINX servers
This is the default error page for NGINX servers.

How to flush rewrite rules?

Before doing anything more complicated to remedy the problem, it is recommended that you flush rewrite rules. All custom rewrite rules are cached by WordPress. Plugins and themes can update those rules, but WordPress will not notice until the cache is reloaded.

All you have to do is follow the steps below.

  1. Go to the admin page “Settings -> Permalinks“.
  2. Save the rewrite rules, clear the cache, and refresh the page by clicking “Save changes“.

Save changes to flush rewrite rules

If everything went according to plan, the .htaccess file should have been generated and the rewrite rules settings should have been purged. However, if the problem has not been resolved and the 404 error page continues to appear, there is one more thing you may do.

How to manually build or restore the .htaccess file?

In order for WordPress to parse and dynamically handle pretty permalinks, the .htaccess file must be present in the WordPress installation. In a nutshell, .htaccess is the configuration file used to control the Apache server with modules like “mod_rewrite” for permalink rewriting.

In some cases, the .htaccess file cannot be saved in the WordPress root directory because of file permissions (CHMODs). The other possibility is that a malfunctioning plugin removed or altered the .htaccess rules. To fix a corrupted .htaccess file, you will need FTP access to the root directory.

.htaccesss file location
The .htaccess file should be in the same folder as the wp-config.php file.

The default configuration file code is shown below. If the .htaccess file is not present, you will need to create it manually. Please copy, paste, and save the file with the default contents provided below.

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
The code snippet above is only suitable for a standard single-domain installation. This code is different if you are running a multisite/network website. Official documentation has further information about the default .htaccess file.

Leave a Reply

Your email address will not be published.