October 15, 2019

PHP caching and .htaccess caching

A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache. In PHP, caching is used to minimize page generation time. PHP basically has two main types of caching: ‘output caching’ and ‘parser caching’. PHP ‘output caching’ saves a chunk of data somewhere that can later be read by another script faster than it can generate it. ‘Parser caching’ is a specific feature. PHP is a scripting language and code is not compiled or optimized to a particular computer. Every PHP file must be parsed and that takes time. This type of time minimization is ‘parser caching’.

The simplest way to implement caching in php, just update your .htaccess file. Here is my current .htaccess caching:

<IfModule mod_headers.c>
    # Cache Media Files
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
        Header set Cache-Control "public"
        Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
        Header unset Last-Modified
    </FilesMatch>

    # Cache JavaScript & CSS
    <FilesMatch "\.(js|css)$">
        Header set Cache-Control "public"
        Header set Expires "Mon, 20 Apr 2015 20:00:00 GMT"
        Header unset Last-Modified
    </FilesMatch>

    # Disable Caching for Scripts and Other Dynamic Files
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
        Header unset Cache-Control
    </FilesMatch>
</IfModule>

After updating the .htaccess file, dont forget to restart your httpd/apache server.

Vedant Singh

Linux System Administrator | Entrepreneur | Founder Techmion. I have started my career as a Linux system Administrator after that quit my job to start something new that excites me and started a Tech Company in order to provide the best technology to my clients. My Company website link: Technology at its best For Bulk, SMS-Marketing follows the link : https://www.pingsms.in/

View all posts by Vedant Singh →

Leave a Reply

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