Website Performance – gZipping & Extended Expirations
With a mixture of a few techniques added to the .htaccess or httpd.conf files, you can reduce the pageload and the bandwith usage of your website fairly easily. In the examples below i’m referring to expressions for addition to .htaccess, the http.conf file may be structured differently to do the same things. I’m setting this up in a standard LAMP setup with Apache 2 and PHP5. It will also wirk with versions of PHP4 but not all the following items work in Apache 1.3.
Apache Module mod_expires#anchor
The first thing to do is extend the amount of time text, images, css, flash and javascript are stored in the users cache. To turn this on we need mod_expires to be active, we then use the expression ExpiresActive On
. We then set a default expiry for everything which is generally set to 6 hours (300 seconds) from the time of access. This is set using the expression ExpiresDefault A300
. Alternatively we can set it to expire a set amount of time since the file was last modified using M instead of A as in ExpiresDefault M300
. Then, if need be, we can take a more granular approach to expiration times by setting expiration by filetype using the expression ExpiresByType _[mimetype][a|m][seconds]_
. So to set GIF images to expire one week from time of access we’d use ExpiresByType image/gif A604800
.
FileETag Directive#anchor
The FileETag directive configures the file attributes that are used to create the ETag (entity tag) response header field when the document is based on a file. Because we’re manually setting expirations we don’t require these headers so it’s easiest to just turn it off using the expression FileETag none
.
Compressing PHP Pages#anchor
The following requires the PHP installation to have the zlib extension enabled, which it should be by default. We use the expression php_value output_handler ob_gzhandler
to turn on compression of the final php pages sent to the users browser. It’s noted on the PHP.net website that turning this on via the php.ini zlib.output_compression
is preferred if available to edit.
Apache Module mod_deflate#anchor
As of Apache 2.0, there’s an available module that compresses the server output before sending to the user, called mod_deflate. This module must be turned on in order to work. I set this up to compress by filetype so it looks like AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/x-javascript application/javascript application/x-javascript
. There are ways to set this up even on a per browser basis but since the browser negotiates with the server before any files are transferred, it’s not necessary. If the browser is not compatible with compressed content, the server will provide the content uncompressed.
It’s also worth noting that Apache version 1.3 had a method for serving compressed content called mod_gzip.
Final Notes#anchor
Using these methods I’ve decreased pageload and bandwith on my site, after adding it my ySlow score for the homepage went from a D(64) to a B(83) and there was a noticeable increase in pageload speed as I was navigating the site. Many people stress that including this code into the httpd.conf file is better because it loads faster and isn’t reloaded every page load as the .htaccess file would be but my being on a shared environment I haven’t tried it myself..
Final Code#anchor
ExpiresActive On
ExpiresDefault A300
ExpiresByType text/javascript A2592000
ExpiresByType text/x-javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A604800
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A2592000
ExpiresByType application/pdf A604800
ExpiresByType text/html A300
FileETag none php_value output_handler ob_gzhandler AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/x-javascript application/javascript application/x-javascript
Need help with an upcoming project?
I'd love to hear more about what you have coming up and how I can help bring it to life! My earliest availability is currently Q1 2025.
Get in Touch