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 #
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 #
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
Compressing PHP Pages #
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 #
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
It's also worth noting that Apache version 1.3 had a method for serving compressed content called mod_gzip.
Final Notes #
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 #