Search

How to Resolve the 404 Error in WordPress

    Sumit Pore
Listen to this article
404-error-WordPress
Error 404 : Page Not Found

As a beginner in WordPress one error that always wrecked up my nerve was the 404 Error. That is why, one thing I have always tried to do is to help fellow developers get rid of this error…. and while trying to do so once, I thought to myself, ‘Why not write a post on it?’. So, here I am once again, with a post which will help you get rid of the 404 error.

However before we go on to figuring out how to get rid of this error we should know why does this error occur in the first place. Well it’s simple, a 404 error is primarily generated by your server when a requested URL is not found. This situation can arise in one of the following three situations.

  • .htaccess file is not getting executed
  • Rewrite module is turned off
  • .htaccess file is not writable

Each of the three scenarios above cause the same error. However, one important thing to note here is that while the error might be the same, the reasons for that error are different and hence have to be dealt with differently. So, let’s take it one at a time and hopefully your issue will be resolved even before this post ends.

.htaccess is Not Getting Executed

The 404 error can occur when the .htaccess file is not getting executed. This issue can be resolved by using the AllowOverride Directive. Connect to your server using SSH and open the configuration file of your domain. It is usually placed in /etc/apache2/sites-available directory.

Are you wondering how to find the correct configuration file? No worries! For explanation purposes let’s consider you have a domain example.com. Now execute the following commands in terminal

cd /etc/apache2/sites-available
grep ‘example.com’ * -l

[space]

The second command will list the configuration file of example.com. Now, let’s assume that the configuration file you have obtained is example.com.conf. Open that file by executing the following command.

sudo nano example.com.conf

[space]
Search for Directory directive in that file. If there are multiple Directory directives in that file, then look for the path provided with that directive. Choose the one which points to the DocumentRoot. So if DocumentRoot for your website is /home/example/public_html, then choose the Directory directive which looks like <Directory /home/example/public_html> In that Directory block, add this line.

AllowOverride All

[space]

After adding above line, save the file and reload Apache using the following command.

sudo service apache2 reload.

[space]

Now check if your links are still throwing 404 errors. If they are, then read further. Even if they are not I would still suggest you to read further and be done with all the issues concerning with the 404 Error.

[space]

Rewrite Module is Turned Off

You are reading this part, means you are still getting the 404 error. So now let’s check if the rewrite module in Apache is enabled or not. How to confirm whether rewrite module is enabled or not? Execute the following command

sudo apache2ctl -M | grep rewrite

[space]

If it prints the following then that means rewrite module is enabled.

‘rewrite_module (shared)’

[space]

If not, then run the below command to enable that module

sudo a2enmod rewrite

[space]

After enabling it, we need to reload apache. So lets fire a command to do just that.

‘sudo service apache2 reload’

[space]

.htaccess file is not writable

In ideal situation you should not get a 404 error when you hit a link and the server is unable to find the page. I say this because, the fact that your .htaccess file is not writable should have become known to you when you changed the permalink structure from default to a pretty permalink. This means that if your .htaccess file is not writable and you change the permalink structure and save it then it throws another error which looks something like this.

htaccess-writable-wordpress
.htaccess File Not Writable

However if you have not paid attention when WordPress was talking then you will be faced with the 404 error very soon. If you have gotten a 404 error and it has not been resolved with either of the above then I would say it’s time to check your .htaccess file permissions. This issue can be resolved by any of the following two methods.

  • Create a .htaccess file by yourself in the root directory.
  • Apply appropriate permissions on the .htaccess file so that if you change the permalink structure in future, then WordPress can automatically write into it.

As the first solution is pretty simple we’ll focus on the second one.

[space]

Apply appropriate permissions on the .htaccess file

First, we need to know the owner who runs apache. Fire up your terminal and connect to your server using SSH. Once you are there, execute the below command

sudo ps -ef | grep apache2 | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'

[space]

Above command would give you the Apache owner. Note it down. For sake of this article, let’s assume that the Apache user obtained is www-data. Hence wherever www-data appears in this article, replace it with the Apache user you got. Now navigate to the DocumentRoot directory of your website in terminal. DocumentRoot is the directory where your WordPress related files are located. Once you are there, run the following commands.

sudo touch .htaccess 
sudo chmod 644 .htaccess
sudo chown www-data .htaccess

[space]

You are done! Now go to your WordPress dashboard and try choosing and saving the permalink you wanted. The error would have disappeared. Check if all links are working correctly.

One more very obvious reason for a bad link is that the visitor has entered a bad link. In such a situation the 404 error is inevitable. However, the situation can be improved by designing a good 404 error page. Now if a visitor does get this error on your website, then at least he would be greeted with a great user friendly page devoid of all the technical lingo!

So, if you thought it was an eyesore each time you had to see a 404 error  for one of the pages on your website, then by now I’m quite confident that the feeling is something of the past. If it isn’t yet then connect with me through the comments section and I will be happy to help 🙂

Sumit Pore

Sumit Pore

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads