Fix Wordpress Posts Returning 404 Error

Page content

How to fix Wordpress Posts returning 404 error

Sometimes after changing the permalink structure or migrating your domain or domain name change, you may find that the posts have stopped showing and you are instead getting 404 error pages. There can be various causes behind it. You might be able to access the admin area and even your home page may be loading fine. However, when you try to access any posts or another inner link such as the archives, you get a 404 error.

In this post, we will show you how to fix when Wordpress posts return a 404 error, There are a few fixes you can try but first of all you will need to check the reasons that may case posts to become 404.

Here are the main reasons that your Wordpress posts might be returning 404 error:

Plugin or theme conflict Code related issues Problem with the .htaccess file Mod_rewrite Please note that fixing this issue may require some technical skills. You might have to make changes inside the .htaccess file as well as make changes on your server.

Plugin or theme conflict:

A plugin or theme conflict can also cause posts to throw a 404 error. IN such a situation, you must switch to a default Wordpress theme like Twenty Twenty. You can also deactivate plugins one by one and check if the issue goes away.

You will be able to find the plugin that is causing the issue. Deactivate all the plugins to see if the problem goes away and then try activating one by one to single out the plugin that might be causing a problem.

You can remove that plugins and replace it with another one or you can contact the plugin author to know about the possible reasons that the conflict might be happening and how to fix it.

Similarly, if you switched to a default Wordpress theme and the problem was gone it might be an issue caused by the theme. So, remove the theme you had been using and either continue using a default theme or switch to a new one. If changing the plugin or theme fixes the issue then fine. Otherwise, you can try the next step.

Code conflict

Suppose, you have newly added a piece of code to your function.php file or another code snippet in the header or body or your website. A problem with this piece of code can cause conflicts on yoru side and may cause the posts to become 404. So, do not insert random codes provided by less reliable sources into your website’s html. Instead only use codes provided by reliable sources and use a plugin like code snippets to add the code to website html.

For example, you had inserted a code that modified some functionality on your website but it also caused a conflict. Remove that piece of code and instead try to use a plugin that offers similar functionality. Instead of adding code manually if you have to use the wp code lite plugin which will also help you find any errors with the code. If this is not the case, you might need to take a look at the permalink settings in your website.

Sometimes, there may be an issue with the rewrite rules in your .htaccess file. In this case, you will just need to flush the rewrite rules which is quite easy. On your Wp dashboard, go to settings -> permalinks and then select post-name and click on save settings. It will automatically flush the rewrite rules and insert the right ones again.

If wordpress is not able to write to the .htaccess file and make changes to the rewrite rules, it means you have not set the right permissions at the server level. You can either change permissions to make the file writable or you can contact your hosting provider to do so. Wordpress will give a warning if this is the case. You can easily change ownership of your root folder and make it writable.

Sudo chown -R $USER:$USER /var/www/html
Sudo chmod 755 /var/www/html

Replace /var/www/html with your root folder. Then again try to save the permalink settings. This must fix the issue.

Manually make changes to .htaccess file

If you want you can also manually edit the .htaccess file and add the code provided by Wordpress to it. You can add this code to the .htaccess file through SFTP or using an .htaccess editor plugin or Yoast plugin.

You will need to insert the following code into the .htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Make the changes and save the file. Thsimust fix the issue. If it did not, then it means you have not enabled .htaccess file on your server and your .htaccess file is not working.

Enable the use of .htaccess file

You will need to make a few changes to your server configuration to enable the .htaccess file so that the changes inside it have effect. However, you will also need to check if the rewrite module is enabled on your server. So, first check if mod_rewrite is enabled on your server. To do that, you can check out a list of modules. Just type the following command into the terminal:

Sudo apachectl -M

You will get an output like the following:

$ sudo apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 expires_module (shared)
 fcgid_module (shared)
 filter_module (shared)
 headers_module (shared)
 mime_module (shared)
 mpm_event_module (shared)
 negotiation_module (shared)
 proxy_module (shared)
 proxy_fcgi_module (shared)
 reqtimeout_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 socache_shmcb_module (shared)
 ssl_module (shared)
 status_module (shared)

You can see that the above list includes the rewrite module which means the module is enabled. However, if it is not, you can enable the module using 👍

sudo a2enmod rewrite

The above command will enable the rewrite module but you need to restart apache server to apply the changes.

sudo systemctl restart apache2

Now, that the apache rewrite module, you will need to make a small change inside the server configuration file for enabling the htaccess file. Open the virtual hosts file for your website:

sudo nano /etc/apache2/sites-available/example.com

Add the following to your Vhosts file after changing the root directory to the one you are using:

<Directory /var/www/html>
AllowOverride All
</Directory>

Replace the /var/www/html with your root directory and restart the apache server.

sudo systemctl restart apache2

Your htaccess file must be working now. You can now insert the code we have provided in the previous step or just flush the permalinks cache by clicking on save settings in the permalinks settings. This will fix the 404 error.

However, in case none of the above methods works for you, there might be an error with your server configuration. Chances are high that one of the above methods will definitely fix it for you. Otherwise, you can contact your hosting provider to check any issues with the server.

Suggested Reading

How to fix Discovered Currently not Indexed

Learn about Google Policies Regarding Spam

How to Host Google Fonts Locally in Wordpress

Fix Crawled Currently Not Indexed

How to Diagnose and Recover from a Drop in Website Traffic/Impressions

How to enable mod_headers on Apache server Ubuntu 22.04

How to enable mod_expires in Apache Server Ubuntu 22.04