Skip to content
Advertisement

Does WordPress trigger index.php when the page is cached?

All pages on my site are cached using the WP Rocket plugin, and everybody is telling me that the root index.php is not triggered when a page is cached.

I went ahead and added this piece of code to my root index.php to track its executions:

error_log( date('[ Y-m-d H:i:s ] ', $_SERVER['REQUEST_TIME'] ) . $_SERVER['REQUEST_URI'] ."n", 3, dirname( __FILE__ ) . "/index.php.log" );

I checked the log and indeed the index.php seems to not trigger on cached pages.

What I don’t understand is that online guides about WordPress loading sequence say that index.php is the very first thing that gets executed when you visit a site. It loads wp-blog-header.php, then wp-load.php sets up the WordPress environment, etc.

https://medium.com/@dendeffe/wordpress-loading-sequence-a-guided-tour-e077c7dbd119 https://www.mikeinmonospace.com/wordpress-internals-loading-sequence/

So what am I missing? Why isn’t index.php triggered when the page is cached? Is there anything triggered before index.php that loads the cached page?

Advertisement

Answer

In WordPress (and most modern CMS’s, in fact) the Front Controller pattern is used which routes all requests for the CMS to a single file, which is often index.php. However, as @CBroe said, this is not the CMS that is doing that, it is a rule in the web server. For an Apache-based web server this is in the .htaccess file, for IIS it might be a web.config file and for Nginx it could be in one of the server directives.

When a caching solution is used, they usually also inject rules into the web server, but before the CMS’s rules, which means that index.php is no longer the first and therefore default rule.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement