What Is a 404 Error Code? How They Happen and Why You Should Fix Them, Hostripples Web Hosting

What Is a 404 Error Code? How They Happen and Why You Should Fix Them

Basically, in this article, we are going to see what is 404 error code, so before going into this let’s understand what is error code. Error code is nothing but a numerical number assigned to known errors which can occur while debugging, running or executing the application/website (basically program code). This can happen due to n-number of reasons, it could be server resources, hardware/software compatibility, or any specific coding syntax. Each error code has its unique number, this article will focus on 404 Error Codes and further we will see how it happen and how we can fix it.

The severity of 404 error vary from small to big which may lead to hammer your Google position and large drop in website traffic too. In simple words, 404 error code is nothing but the resources/web pages or link that we trying to look on web browser does not exist or match with request send on server.

Related: How to Fix the ERR_CONNECTION_TIMED_OUT Error

Let’s take the example of the postman, Mr. Smith has sent a letter to Mr. Bean at the address where he was living a few years back but Mr. Smith was not aware that Mr. Bean had already moved from that address and shifted to a new city.

What Is a 404 Error Code? How They Happen and Why You Should Fix Them, Hostripples Web Hosting

Now the postman is doing his job by trying to reach Mr. Bean at his old address and trying to find him to hand over the letter.  In this situation, we can say that Mr. Bean “NOT Found (404)” at the address the postman is looking for.

In the same way, if you read the above example then you will understand it more clearly. As of now check what is 404 error and why this error occur or happen.

Inaccurate URL / File / Resources (images/path)

An inaccurate URL, file, or resource is one of the primary reasons for a 404 error. When a user enters a URL in a web browser or clicks on a link, the server checks whether the requested resource exists on the server. If the requested URL is mistyped, points to a non-existent file, or references a resource that has been removed, the server responds with a 404 error to the requester to indicate that the requested page is not found.

Old References / Moved Contents from web pages:

Old references or moved content from web pages can indeed lead to 404 errors. When a webpage is moved, renamed, or deleted, and there are existing links or references pointing to the old location, users attempting to access those links will encounter a 404 error. This occurs because the server cannot find the requested resource at the specified URL

Missing Pages or Human Error:

The occurrence of 404 errors due to missing pages or human error is not uncommon. This happens when a page that was supposed to exist is either not created, has been accidentally deleted, or is not uploaded to the server. Human errors, such as mistakes during website development, content management, or file organization, can contribute to the absence of the intended pages.

Hosting/Server/Application Misconfiguration:

A 404-error due to hosting/server/application misconfiguration occurs when the web server or the hosting environment is not properly set up to handle requests for a specific URL or resource. This can be caused by various factors related to the server or application setup. This could be a reverse case to the code is not coded properly to match the hosting environment/server resources or application settings. Regular maintenance, testing, and collaboration with hosting providers are key practices for minimizing the impact of misconfigurations

Read: Managed WordPress Hosting VS VPS Hosting: Which one is best for your business?

DNS (Domain Name Service) Misconfigured:

A 404-error due to DNS (Domain Name System) misconfiguration occurs when the DNS settings for a domain are not properly configured, preventing the browser from resolving the domain name to the correct IP address. The DNS is responsible for translating human-readable domain names into machine-readable IP addresses, allowing users to access websites by domain names. Proper DNS configuration is essential for the overall accessibility and functionality of a website, and regular checks can help ensure its reliability.

How to Fix 404 Errors?

Till now we have seen what is 404 error code and why it occurs, now let’s move further and understand the process of Fixing a 404 error on an Apache server with WordPress this includes two main elements: troubleshooting both server and application-level issues.

What Is a 404 Error Code? How They Happen and Why You Should Fix Them, Hostripples Web Hosting

Check the Permalink Settings in WordPress:

Permalink settings in WordPress refer to the structure of URLs for your posts and pages. WordPress allows you to customize how your URLs appear, making them more user-friendly and search engine-friendly but sometimes working on this causes a 404 error.

To correct it please follow the below process

Log in to your WordPress admin dashboard.

Go to “Settings” and then “Permalinks.”

Ensure that your desired permalink structure is selected. Click “Save Changes” even if no changes are made.

Check .htaccess File:

Yes, the .htaccess file is important in WordPress, as it plays a crucial role in configuring various settings related to URL rewriting and redirection on Apache web servers. The file is used to define rules that control how the server handles different types of requests.

You can Navigate to the root directory of your WordPress installation using an FTP client or file manager. Locate the .htaccess file. If it doesn’t exist, you may need to generate it.

To generate the WordPress .htaccess file please follow the below steps:

In your WordPress admin, go to “Settings” > “Permalinks” and click “Save Changes” to regenerate the .htaccess file.

If the file already exists, check its contents. WordPress may add rewrite rules to this file to enable pretty permalinks.

Verify File and Directory Permissions:

Ensure that file and directory permissions are set correctly. The web server needs appropriate permissions to read files and execute scripts.

Common permissions for directories are 755, and for files, it’s 644. You need to make sure that you adjust these permissions as needed, but be attentive while handing permission it should not be open permission like 777 which may lead to trouble for security reasons.

Flush Rewrite Rules:

If you are experiencing persistent 404 errors or if you’ve made changes to your WordPress site that affect permalinks, you may need to flush the rewrite rules. This process ensures that the server is aware of the changes and regenerates the rewrite rules in the .htaccess file.

Here’s how you can flush rewrite rules in WordPress:

Log in to your WordPress Admin Dashboard:

Go to your WordPress admin area by visiting your domain followed by “/wp-admin/” (e.g., http://yourdomain.com/wp-admin/).

Navigate to Permalinks Settings:

In the WordPress dashboard, go to “Settings” and then click on “Permalinks.”

Save Permalink Settings:

Without making any changes, click the “Save Changes” button at the bottom of the Permalinks settings page.

This action triggers the flushing of rewrite rules, and WordPress will regenerate the rules based on your permalink structure.

Check for 404 Errors:

After saving the permalink settings, test your website to see if the 404 errors persist. In many cases, this simple action can resolve issues related to permalink changes or persistent 404 errors.

If flushing the rewrite rules through the permalinks settings does not resolve the issue, you can manually flush the rewrite rules using code. This is often done when working with custom plugins or themes. Here’s an example of how to do it:

Add Code to Your Theme’s functions.php:

Open your theme’s functions.php file for editing.

Add the following code at the end of the file:

What Is a 404 Error Code? How They Happen and Why You Should Fix Them, Hostripples Web Hosting

Save the File:

Save the changes to the functions.php file.

Remove the Code:

After visiting your website and confirming that the issue is resolved, remove the added code from the functions.php file. It’s essential to remove this code to avoid unnecessary rule flushing on each page load.

Note: The code above triggers the flush_rewrite_rules function on every page load, which is not recommended for production sites. It is intended for troubleshooting purposes. After resolving the issue, remove or comment out the code.

IMP : Always be cautious when modifying code, and consider making a backup of your site before making changes to important files.

Check Virtual Host Configuration:

If you have access to the server’s virtual host configuration, ensure that it’s configured correctly. Look for the AllowOverride directive, and make sure it’s set to at least FileInfo to allow the use of .htaccess files.

Check Mod_Rewrite Module:

Ensure that the Apache mod_rewrite module is enabled. You can check this by running the following command as root user from the command prompt:

sudo a2enmod rewrite

Then, restart the Apache service:

sudo service apache2 restart

Update Site URL in WordPress Settings:

The site URL which points to the URL on which the WordPress installation going to map this URL should be correct you can check this below with this step:

In the WordPress admin, go to “Settings” > “General.”

Check and update the “WordPress Address (URL)” and “Site Address (URL)” if necessary.

Check for Plugin or Theme Conflicts:

Checking for a plugin or theme conflicts is a crucial step in troubleshooting and resolving 404 errors in WordPress. Incompatibilities or issues with plugins or themes can sometimes lead to URL-related problems, including 404 errors.  You may temporarily deactivate plugins and switch to a default WordPress theme (e.g., Twenty Twenty-One). Check if the 404 error persists. If not, reactivate plugins and themes one by one to identify the culprit.

Verify DNS Configuration:

Ensure that your domain’s DNS settings are correctly configured to point the correct server. Also, verify that your domain is using the correct nameservers. Nameservers are responsible for translating your domain into an IP address. Contact your domain registrar to confirm the correct nameserver settings.

Read: List of Top 20 WordPress Plugins for Bloggers: Updated

Final Words

Don’t be afraid of the 404-error code!

In this blog post, we break down what a 404-error code is, how it happens, and most importantly, how to fix it.

Say goodbye to those pesky broken links for good.

What Is a 404 Error Code? How They Happen and Why You Should Fix Them, Hostripples Web Hosting
Andrew J