diTii.com Digital News Hub

Sign up with your email address to be the first to know about latest news and more.

I agree to have my personal information transfered to MailChimp (more information)


.htaccess Most Essential Blog Optimization Rules (mod_gzip or mod_deflate)

This blog post discuss blog optimization process using .htaccess rules. You must adde these rules to your root .htaccess (HyperText ACCESS) file. .htaccess Basics

Compress Content:

This next code utilizes Apache’s mod_gzip and newer mod_deflate code to compress files on the fly. As compressed files are smaller, less data must be downloaded to your readers browser and less data travels faster.

1. Test What Apache mod_ Is Active On Your Server

Your server probably has mod_gzip (Apache 1.3) or mod_deflate (Apache 2.x, preferred!) active. To test which you can / should use:

1a. Create index.shtml.txt file with one line (#printenv inside a html comment):

<!--#printenv -->

1b. Create .htaccess file with these lines:

SetEnv MOD_mod_deflate 0
SetEnv MOD_mod_gzip 0
SetEnv MOD_mod_headers 0
SetEnv MOD_mod_expires 0

SetEnv MOD_mod_deflate 1

SetEnv MOD_mod_gzip 1

SetEnv MOD_mod_headers 1

SetEnv MOD_mod_expires 1

1c. Create a temporary directory to your server, e.g. http://example.com/temp/

1d. Transfer index.shtml and .htaccess to temp-directory

1e. Access index.shtml.txt, e.g. http://example.com/temp/index.shtml.txt

1f. You’re looking to find either MOD_mod_deflate=1 OR MOD_mod_gzip=1 (and MOD_mod_headers=1 + MOD_mod_expires=1) on the page. 1 means the module is enabled. 0 means the module isn’t enabled.

1g. Delete temporary directory

Now, use the mod which ever is active, either mod_gzip or mod_deflate compression in .htaccess:

1.2. mod_gzip Compression:

# mod_gzip compression (legacy, Apache 1.3)

mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|xml|txt|css|js)$
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.*


1.3. mod_deflate Compression:

# BEGIN Compression (DEFLATE)

# Enable compression
AddOutputFilterByType DEFLATE text/css text/javascript application/x-javascript text/html text/plain text/xml image/x-icon

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Make sure proxies deliver correct content
Header append Vary User-Agent env=!dont-vary
# Ensure proxies deliver compressed content correctly
Header append Vary Accept-Encoding

# END Compression (DEFLATE)

NOTE: All AddOutputFilterByType rule should be on one line (next line always starts with the comment # Dont’ compress…) , even if it wraps above.

1.3. Test That Compression Is Working:

To test if compression is working on a specific file or URL: http://www.whatsmyip.org/http_compression/

2. Set Expire Headers and Times

2.1. Disable ETags

# No ETags, No Pragma

Header unset Pragma
Header unset ETag

FileETag none

2.2. Set Expiration Times For Caching

Choose any options, depending if you’ve mod_expires, mod_headers or both enabled. Feel free to tweak your own using these as a guideline:

2.2.1. mod_expires & mod_headers


# Default cache time to 1 year (31536000 sec)
Header set Cache-Control "max-age=31536000, public, must-revalidate"

# Turn on Expires
ExpiresActive On
# set default to "access plus 1 year"
ExpiresDefault A31536000
# html - "modification plus 1 hour"
ExpiresByType text/html M3600
# css and JavaScript - "modification plus 6 weeks"
ExpiresByType text/css M3628800
ExpiresByType text/javascript M3628800
ExpiresByType application/x-javascript M3628800

# No cache for php-files

ExpiresActive Off

Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"


2.3. Alternate Way, Using ETags

If you use this, don’t disable ETags as shown at 2.1.

FileETag MTime Size

ExpiresActive on
ExpiresDefault "access plus 1 year"

2.4. (Extra) Use Minimal Caching During Maintenance

Very useful when you’re doing changes to your site and want to see the changes without doing a forced reload everytime.

# implement minimal caching during site development

Header set Cache-Control "max-age=5"

ExpiresActive On
ExpiresDefault A5

Share This Story, Choose Your Platform!

Get Latest News

Subscribe to Digital News Hub

Get our daily newsletter about the latest news in the industry.
First Name
Last Name
Email address
Secure and Spam free...