Month: July 2009

Apache configuration for ETags, Gzip and Expires Header

Hi,

After digging deep into the sites and configuring my apache server ( after so many failures )
Here i came up with list of things to be done for

  1. Gziping the CSS/JS, image and flash files
  2. Removing Etags from CSS/JS and Image/flash files.
  3. Adding expires header to CSS/JS and image/flash files.

First, Go to your apache’s httpd.conf file
I my case its “C:\xampp\apache\conf\httpd.conf”

The lines beginning with # indicate that these are comments
Remove the comments for these below mentioned lines

LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

Add the below thing to remove the ETags. This can be added at the end

FileETag None

To Add expires header to all the files that are requested from the server

ExpiresActive On
ExpiresDefault "access plus 300 seconds"
<Directory "/myProject/webResources">
    Options FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    ExpiresByType text/html "access plus 1 day"
    ExpiresByType text/css "access plus 1 day"
    ExpiresByType text/javascript "access plus 1 day"
    ExpiresByType image/gif "access plus 1 day"
    ExpiresByType image/jpg "access plus 1 day"
    ExpiresByType image/png "access plus 1 day"
    ExpiresByType application/x-shockwave-flash "access plus 1 day"
</Directory>

Note: Here “myProject” is the folder where my web pages are present. i.e “C:\xampp\htdocs\myProject”. Where “webResources” is the directory where my css, js, image, flash files are present. For all these files I am adding expires header of one day. Means these files can be can be cached for the entire day in the local browser. In other words these files are not changed at least for one day.

To Gzip the CSS and JS files that are present in the “webResources” folder

<Location "/sprintCommunity/webResources">
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

Please post a comment if this was useful to you. Thanks

Advertisements