Categories: PHP & Frameworks

Enabling Gzip Compression of PHP, CSS, and JS Files Without mod_deflate

For enable Gzip compression using mod_deflate add following lines to your .htaccess file AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript But those who don’t allow the mod_deflate module and run PHP in CGI/FastCGI mode you can’t go with the easy method. So, to serve up your PHP, CSS, and JS files you can try the following method.

Step 1: PHP Configuration Add or modify the following lines in your custom php.ini file

output_handler = Off
zlib.output_compression = On
zlib.output_handler = ob_gzhandler

Now this will take care of gzipping all PHP files. Step 2: .htaccess Configuration Add the following lines to the bottom of a .htaccess file in the root of your website.

RewriteEngine On
RewriteRule ^(.*\.js) gzip.php?type=js&file=$1
RewriteRule ^(.*\.css) gzip.php?type=css&file=$1

This will redirect all css and js requests files through gzip.php, which we will create in the next step. Step 3: File Processing PHP Script The following PHP script will inherently use the PHP compression you’ve already enabled and also add headers to your files take advantage of your client’s browser cache to make subsequent loads faster. Create a file named gzip.php in your website’s root and add the following lines to it:

<?php

if(!ini_get('zlib.output_compression')){ die(); }

 $allowed = array('css','js');

 if(isset($_GET['file']) && isset($_GET['type']) && in_array(substr($_GET['file'],strrpos($_GET['file'],'.')+1), $allowed)){
  $data = file_get_contents(dirname(__FILE__).'/'.$_GET['file']); // grab the file contents

  $etag = '"'.md5($data).'"';
  header('Etag: '.$etag);

  switch ($_GET['type']) {
  case 'css':
   header ("Content-Type: text/css; charset: UTF-8");
  break;

  case 'js':
   header ("Content-Type: text/javascript; charset: UTF-8");
  break;
  }

  header('Cache-Control: max-age=300, must-revalidate'); //output the cache-control header
  $offset = 60 * 60;
  $expires = 'Expires: ' . gmdate('D, d M Y H:i:s',time() + $offset) . ' GMT'; // set the expires header to be 1 hour in the future
  header($expires); // output the expires header

  if ($etag == $_SERVER['HTTP_IF_NONE_MATCH']) {
   header('HTTP/1.1 304 Not Modified');
   header('Content-Length: 0');
  } else {
   echo $data;
  }
 }
?>

Great! With these steps in place your css and javascript files will be processed by gzip.php and output using PHP’s gzip compression library (zlib). This method can be extended to more filetypes by adding to the allowed file types in gzip.php and adding more lines to your .htaccess file.

Developer Diary

Share
Published by
Developer Diary

Recent Posts

The Power of Email Marketing: Boosting Your Business’s Success

Introduction Even with the abundance of digital communication channels available today, email marketing is still…

13 hours ago

Laravel vs Node Js: Which One Is Good For What?

Introduction In the world of web development, selecting the correct framework is critical. Laravel and…

3 months ago

Docker Cheatsheet: Essential Commands and Explanations

Introduction By enabling containerization, Docker has transformed the way software is built, deployed, and managed.…

8 months ago

Difference between Memcached and REDIS – Secrets of Caching

Introduction Speed and efficiency are critical in the ever-changing world of web development. Effective caching…

8 months ago

How to Revert a Git Commit: A Simple Example

Introduction Git, a popular version control system in software development, enables developers to track changes,…

8 months ago

What is Git Stash and Why Do You Need It

Introduction Are you tired of grappling with tangled changes and unfinished work while using Git?…

8 months ago