Compressing js & css files where mod deflate not available

Compressing js & css files where mod deflate not available

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Compressing js & css files where mod deflate not available

Compressing JavaScript & CSS Files on Shared Hosting: A Practical Guide

Running an e-commerce platform like Magento on shared hosting can present unique challenges, particularly when it comes to optimizing your store’s performance through file compression. While the standard approach of using the .htaccess file for mod_deflate is often recommended, this may not be available on all shared hosting plans.

Understanding the Challenge

Many shared hosts restrict the use of .htaccess files or disable certain Apache modules like mod_deflate. This makes it difficult to compress JavaScript and CSS files directly at the server level. However, there are alternative methods that can achieve similar results with a bit more effort.

The PHP Compression Method: An Alternative Solution

One such method involves using PHP to compress your text-based files before they are served to the client. This approach was outlined in this old but still relevant blog post: How To Compress PHP And Other Text Files.

The key idea is to create a .htaccess file that forces all JavaScript and CSS files to be served by a PHP script instead of directly. This PHP script then compresses the file before sending it back to the client.

Implementing the Hierarchical Approach

However, as you rightly pointed out, creating a .htaccess file in every directory with CSS or JS files can be quite cumbersome and difficult to manage, especially for a large application like Magento where the file structure is complex. Thankfully, there are ways to implement this hierarchically.

Step 1: Create a Central Compression Script

Instead of creating multiple .htaccess files, create a single PHP script (let’s call it compress.php) that handles the compression. This script should take the path of the file as an argument and return the compressed content.

Here’s a simple example:

<?php
$file = $argv[1];
$content = file_get_contents($file);
header('Content-type: '.$mime_type);
header('Content-Length: '.strlen($content));
echo gzinflate(substr(base64_decode(str_replace(array(" ","+"),array("/","_"),chunk_split(base64_encode($content), 20, " "))), base64_decode(str_replace(array(" ","+"),array("/","_"),chunk_split(base64_encode($content), 57, "+"))));
?>

Step 2: Update Your .htaccess File

In your root .htaccess file, add a rule to redirect all requests for JavaScript and CSS files to the compress.php script.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} \.(js|css)$ [NC]
    RewriteRule ^(.*)$ compress.php?file=$1 [L]
</IfModule>

Step 3: Zipping Your Files

Now, you need to zip all your JavaScript and CSS files. You can do this manually or use a script. The key is that the zipped files should be named as original_filename.js.gz and original_filename.css.gz.

Conclusion: Achieving Hierarchical Compression

While this approach requires more setup than using .htaccess alone, it provides a more maintainable solution for complex applications like Magento. By centralizing the compression logic in a single script and using the .htaccess file to redirect only the necessary requests, you can achieve efficient compression without having to modify every directory.

Remember, proper file compression is a critical part of optimizing your e-commerce website’s performance. It reduces bandwidth usage, improves load times, and contributes to a better user experience.

If you’re looking for more advanced techniques in Magento optimization, consider checking out Laravel Company where we regularly publish articles on best practices and performance enhancements for Laravel applications and related technologies.

Performance API Extensions Themes

Our network