# compress all text and html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# Or, compress certain file types by extension:
<Files *.html>
SetOutputFilter DEFLATE
</Files>
Code language: HTML, XML (xml)
You can compress all type of content (image, css, js etc) using mod_deflate. Copy following code in htaccess to do this. <Location />
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>
Code language: HTML, XML (xml)
Also, you can add following code in your htaccess file and enable compression using mod_gzip. <IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
Code language: HTML, XML (xml)
This technique only works if mod_gzip or mod_deflate modules are loaded in Apache. In our case, these modules were not there and our hosting provider refused to load it as we were using a shared hosting. So following can be another way of enabling compression. if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else
ob_start();
Code language: PHP (php)
Above code will check whether your browser supports gzip, if yes, then it send ob_gzhandler method as handle to ob_start method which buffers the output. Thus output is compressed using ob_gzhandler. Only problem with this method is that you have to manually edit all PHP files or should have a header.php file that gets included in all files. There are still ways to achieve this without touching your PHP files. Read following trick. <FilesMatch "\.(txt|html|htm|php)">
ForceType application/x-httpd-php
php_value auto_prepend_file /the/full/path/gzip-enable.php
</FilesMatch>
Code language: HTML, XML (xml)
But what if you don’t want to prepend a PHP file? Still there is a way to specify default output handler using htaccess. Use following line in your htaccess file to tell your apache to register ob_gzhandler handler function as output handler. Code language: HTML, XML (xml)php_value output_handler ob_gzhandler
// initialize ob_gzhandler function to send and compress data
ob_start ("ob_gzhandler");
// send the requisite header information and character set
header ("content-type: text/css; charset: UTF-8");
// check cached credentials and reprocess accordingly
header ("cache-control: must-revalidate");
// set variable for duration of cached content
$offset = 60 * 60;
// set variable specifying format of expiration header
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
// send cache expiration header to the client broswer
header ($expire);
Code language: PHP (php)
Now add following lines in your htaccess file to enable compression for CSS files. <FilesMatch "\.(css)">
ForceType application/x-httpd-php
php_value auto_prepend_file "/the/full/path/of/this/file/gzip-css.php"
</FilesMatch>
Code language: HTML, XML (xml)
Whenever a http request for .css will come to a server, the type of css will get converted to application/x-httpd-php, thus making them parsed using PHP. Then a file gzip-css.php will be prepend to this request which in turns compress the output using ob_start (“ob_gzhandler”) method. // initialize ob_gzhandler function to send and compress data
ob_start ("ob_gzhandler");
// send the requisite header information and character set
header ("content-type: text/javascript; charset: UTF-8");
// check cached credentials and reprocess accordingly
header ("cache-control: must-revalidate");
// set variable for duration of cached content
$offset = 60 * 60;
// set variable specifying format of expiration header
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
// send cache expiration header to the client broswer
header ($expire);
Code language: PHP (php)
Also add following lines in your htaccess file. <FilesMatch "\.(js)">
ForceType application/x-httpd-php
php_value auto_prepend_file "/the/full/path/of/this/file/gzip-js.php"
</FilesMatch>
Code language: HTML, XML (xml)
Do you know other methods of compressing the output of PHP/JS/CSS files? Let me know, I will try to add them in this tutorial. Happy Compressing :) Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Nice picture dude..!!!
Good stuff! Remember, though, that you are preventing your users from caching your CSS and JS. You should check your ratio of new/returning visitors before considering this.
Awesome article, thanks! Will come in handy in future projects :)
@Gaurav, Thanks for the comment.
@Eric, I forget to mention it in this tutorial. Thanks for pointing this out. I will update the post.
@Disaster, You welcome :) You can bookmark this tutorial if you find it useful.
Thank you so much!
Great article...!
It sounds good news to me. But in Joomla 1.5 there seems no effect....
Is it also worked in Joomla! 1.5 with sef enabled?
Hi Carson,
I have not tried this with Joomla, but I think it will work as these are the normal rules under htaccess file. These rules are applied after Joomla sends the output.
Hi Viral,
Nice tutorial. I would like to know that if there any solution to view this compressed file as normal file ? i have tons of files which are compressed. i need to modify their code for my purpose but due to files are compressed. i am not able to do it. if you know than let me know.
Thanks
Thanks Viral,
It helped me also.