Three Ways to Flush the Rewrite Cache in WordPress

Three Ways to Flush the Rewrite Cache in WordPress

Sometimes you might find that your blog pages are returning a 404 error. On further investigation, you find out that the homepage itself is working just fine and is perfectly accessible. Moreover, if you try and visit your pages via the ID number, there are no problems. It’s only when you try and view them via the “pretty permalinks” that you encounter issues. If this is the case, there’s no need to panic. The problem is most likely caused by an out of date rewrite rules cache. Solving this problem can be easy or hard depending on how much access you have to your WordPress installation as well as the backend.

In this article, we look at three different ways to flush the WordPress rewrite rules cache – one directly from the GUI, the other straight from the database, and the last one as a simple PHP command.

Flushing Rules from the WordPress Dashboard

WordPress allows us to create human recognizable URLs with posts bunched together under categories or archives. Every time you make a change to these structures, WordPress flushes the cache automatically. So to force WordPress into doing this without making any changes, we simply hit the save button.

In the WordPress administration dashboard on the left-hand side, click “Settings” and then “Permalinks”. Scroll down to the bottom of the page after all of the existing options and click “Save Changes”.

flush settings GUI

If you have access to your .htaccess file, WordPress will automatically flush the cache and make any necessary changes. This is the simplest and most straightforward way to flush the rewrite rules cache. But in case it doesn’t work, we can achieve the same effect directly via the database.

Deleting the Cache from the Database

Needless to say, you need to be extremely careful when exercising this option. Ideally, you should take a backup of your entire database before making any changes here. One wrong move could kill your entire WordPress installation. So be warned! Having said that, flushing the rewrite rules this way is quite simple and only requires you to delete the value of a single field. Access your database either via a third-party client or through phpMyAdmin by going through cPanel as shown here:

phpmyadmin

Locate the database in which WordPress resides and hit the SQL tab.

type in sql query

Now type the following into the text box:

SELECT * FROM  `wp_options` WHERE  `option_name` =  'rewrite_rules'

Hit “Go” on the bottom right to execute the command. This should return exactly one row. Click the “Edit” button.

edit it

This will bring up all of the editable fields in this row. The one we are interested in is called “option_value” containing a bunch of alphanumeric strings like this:

just delete

Simply delete everything inside this field and save your changes. That’s it – the next time you access your WordPress site, it should automatically get rebuilt. This is a direct removal of the WordPress cache via the backend. But if you want a programmatic way to do it, we have that as well.

Flush Rewrite Cache via PHP

One important thing to remember is that this operation is an expensive one in terms of resources. You don’t want it running automatically every time you load WordPress. Hence, this command needs to be attached to a relatively rare occurrence like say a plug-in activation or deactivation. The PHP function that flushes the rewrite cache is:

flush_rewrite_rules();

As I said before, it’s not a good idea to simply add this to the end of your functions.php file since that would make it run almost continuously. Instead, either check for some pre-existing condition before allowing it to execute or hook it into an action where it will be necessary.

These three ways of flushing your rewrite cache should give you enough options for fixing your WordPress site in case you’re unable to access the pages via the regular permalinks.

One Reply to “Three Ways to Flush the Rewrite Cache in WordPress”!

Leave a Reply

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.