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.

What are rewrite rules?

Rewrite rules are an essential part of how WordPress processes and interprets URLs, and they play a crucial role in ensuring that your site functions correctly and displays the appropriate content to users. They are created and updated automatically by WordPress as you make changes to your site, such as registering new custom post types or taxonomies.

So, how exactly do they work? Simply said, when a visitor requests a URL, WordPress use rewrite rules to decide which content should be shown. It does this by extracting the slug from the URL and using it to search for the corresponding content in the database.

For example, if a visitor accesses the URL https://example.com/lorem/ipsum, WordPress will use the rewrite rules to determine that the user is requesting a page, and it will extract the slug "ipsum" from the URL. It will then use this slug to search for the corresponding page in the database, and if it finds a match, it will display the page to the visitor.

A sample rewrite rule in action
When a visitor opens the URL, WordPress will use the rewrite rule to figure out what content type needs to be loaded and extract the slug from the URL.

How to Fix WordPress 404 Page Not Found Error?

If you are reading this, chances are you are seeking for a solution to the error that appears when you try to open any subpages on your WordPress website. The most likely cause of this issue is that the .htaccess file is missing from the root directory of the website.

If the rewrite rules are not functioning properly in a standard server environment, the following error will be shown.

The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

To ensure that we are discussing the same issue, please review the photos below. This article discusses 404 error messages such as the ones shown in the screenshots below. Of course, this is not the only option available, as server settings may cause the default WordPress 404 error page to look different.

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.

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. To resolve this issue it is necessary to create a .htaccess file in the root directory of the website and add the necessary configuration for the website.

Happily, WordPress offers an automated process for this task without the need to manually edit any code in files. To take advantage of this feature, simply follow these three steps.

Go to "Settings -> Permalinks" admin section

Enable pretty permalinks

Save the settings to flush the rewrite rules and generate .htaccess code

If the 404 error only appears for particular URLs, skip this section of text and proceed to the next step. If none of the URLs for the subpages can be accessed, you will need to activate "pretty permalinks". Simply put, you will need this to force WordPress to produce the .htaccess file and follow the rewrite rules.

In order to ensure that your pages and posts are accessible, it is essential to configure the WordPress permalink settings correctly. To do so, you should navigate to "Settings > Permalinks" within your WordPress dashboard.

Default permalink settings

It is essential to note that the "Plain" option should be pre-selected when WordPress is installed. In brief, URLs in this mode follow the basic format of ?p=1256 where 1256 represents the post's ID number in MySQL database.

These often look like numbers and strings of text, making them difficult for readers to remember. Pretty permalinks allow you to customize your URL structure so that it is easier for visitors and search engines alike to read and understand what each page contains.

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/

Enabling pretty permalinks in WordPress is a simple process. On this page you can choose from one of several predefined structures or create a custom structure for your specific needs by using tags such as %category% or %postname% .

Enable pretty permalinks

All you need to do is to select any option other than "Plain" (e.g., "Post Name"). Once you are happy with your selection click save changes at the bottom of the screen.

By making this change, the URL will be transformed from its current "raw" format into one that is easier to read. When changing the permalink structure, WordPress will automatically generate an .htaccess file with proper code preventing any 404 errors from occurring when attempting to access content on your website.

How to flush rewrite rules?

If you have just enabled pretty permalinks as outlined in the preceding section of this article, there is no need to proceed further with this step.

It is recommended that you begin by flushing rewrite rules to address the issue before attempting more complex solutions. Flushing the rewrite rules cache may resolve 404 errors caused by plugins or themes updates. It is possible that the issue originated from WordPress utilizing an outdated, cached version of rewrite rules.

To flush the cache, please follow these steps:

  1. Go to the admin page "Settings -> Permalinks".
  2. Flush the rewrite rules and clear their cache by clicking "Save changes" even if no change has been made.

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. Required fields are marked *