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

The "404 error" is one of the most typical problems with permalinks. Usually, the issue is caused by incorrect rewrite rule settings. If that is the case, flushing the rewrite rules should fix the problem.

This issue, which we will describe in this article, often occurs after installing a new WordPress plugin or website that makes use of custom post types.

With admin access, you should be able to simply handle this problem since it can probably be fixed from the WordPress dashboard. However, if the problem is caused by anything other than WordPress settings, such as a misconfigured server or .htaccess file, further investigation is required.

What Are Rewrite Rules?

Rewrite rules are an important part of WordPress since they parse URLs to determine if they match any of the URL formats assigned to content types. You might not even know they exist because they are generated automatically whenever you declare new custom post types or taxonomies.

When a URL matches any of the rewrite rules, WordPress uses the extracted slug to determine which content item should be shown. Each content type has its own distinct URL format. This lowers the amount of database requests and helps WordPress to limit the query to items belonging to a single post type or taxonomy.

For example, if a visitor access the URL below, WordPress will recognize that this URL format is used by "page". It will then search the database for the page matching to the extracted "ipsum" slug. If it detects a match, it will show the page to the visitor. Otherwise, a "404 error" will be shown.

https://example.com/lorem/ipsum

A sample rewrite rule in action

How to Fix WordPress 404 Page Not Found Error?

If you see an error while trying to open any subpages, the most probable culprit is a missing .htaccess file in the website's root directory.

Therefore, if the following error is shown, it seems that the rewrite rules are not operating as they should be. However, this is not the only probable cause of the issue. The default WordPress 404 error page may appear vary depending on server settings.

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.
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.

As long as you have access to the admin panel, fixing the 404 issue should be a quick process. To fix this problem, you can create a .htaccess file in the website's root directory and paste there the default code.

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.

To make sure your pages and posts can be found using their URLs, go to "Settings > Permalinks" in 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 make the URL structure simpler to understand. It helps visitors and search engines to determine the contents of each page based on its address.

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

At this point, the .htaccess file ought to have been created and the rewrite rules flushed. However, if the issue remains and the 404 error page appears, you may try repeating the procedure manually using FTP.

How to Manually Build or Restore the .htaccess File?

In order 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 .file cannot be saved because of file permissions (CHMODs). The other possibility is that a malfunctioning plugin removed or altered it. Fixing this will need FTP access to the root directory.

.htaccesss file location
Both the wp-config.php and .htaccess files should be located in the same directory.

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.
Avatar for Maciej BisAbout Maciej Bis

Maciej has over 10 years of WordPress expertise and is the author of Permalink Manager, a popular WordPress plugin for managing permalinks. He has extensive experience in WordPress programming and has worked with clients from all around the world.

Leave a Reply

Your email address will not be published. Required fields are marked *