FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Speed Tips: Turn On Compression

by 26 comments

mod_deflate disabledApache 2 uses mod_deflate to quickly and easily compress your static .css and .js files before you send them to a client. This speeds up your site like crazy!


.htaccess media caching

FWIW, this is how I configure Apache to set the Expires header:

Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>

The reason I remove and disable the ETag is because supposedly some browsers will ignore your Expires header when it's present:

The reason I remove the Last-Modified header is for the same reason:

The reason I set the Cache-Control header to 'public' is so the browser will cache media over HTTPS (see tip #3):

The reason I set the Cache-Control header to 'no-transform' is to prevent proxies from modifying my content.

--Bil

Turn Gzip Compression On

This goes in your root .htaccess file but if you have access to httpd.conf that is better.

This code uses the FilesMatch directive and the SetOutputFilter DEFLATE directive to only target files ending in .js or .css

<ifModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</ifModule>

Without mod_deflate

mod_deflate disabled


With mod_deflate

mod_deflate enabled


Bandwidth Savings

mod_deflate bandwidth savings

Conditional Compression

# Compress everything except images
 
<location />
# 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
# 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>

Tags

September 5th, 2007

Comments Welcome

  • Ajay

    My site throws up a 500 error when I do this. Any suggestions?

  • http://www.ptaquatics.com Paul

    My site ALSO throws an Error 500.
    I can't see a problem with the code, so something has to be set at the server to accept this, right?

  • Isaiah

    If you get an 500 internal server error, means that your server don't have mod_expire.so and mod_headers.so loaded. Edit your httpd.conf and add these lines :

    LoadModule expires_module /usr/lib/apache2/modules/mod_expires.so
    LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

    Cheers

  • Jason

    When I added the above code, I was given an internal server error. I am not sure why? Any suggestions?

    Header set Cache-Control "max-age=29030400, public"
     
    Header unset ETag
    FileETag None
     
    SetOutputFilter MOD_DEFLATE
  • http://www.askapache.com/ AskApache

    @ Everybody

    Sorry for the delay people, I just updated the code to prevent the 500 error, which is telling you that you don't have the deflate module. At least now you know which module you are missing!

    <IfModule mod_deflate.c>
    <FilesMatch ".(js|css)$">
    SetOutputFilter DEFLATE
    </FilesMatch>
    </IfModule>
  • Jason

    I am trying to add gzip compression for a friends site which is WP. I already set up compression for php files using php.ini. However, I can't get the js|CSS to work. Any suggestions. The site is hosted with Godaddy, apache of course.

  • http://www.askapache.com/ AskApache

    @ jason

    Sounds like you don't have mod_deflate. I need more info like error messages, how you know its not working, and current .htaccess if I were going to answer.

  • Jason

    Thanks for your attention:

    My .htaccess file:

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
     
    #12 Months
     
    Header set Cache-Control "max-age=29030400, public"
     
    Header unset ETag
    FileETag None
     
    # 2 HOURS
     
    Header set Cache-Control "max-age=7200, must-revalidate"
     
    SetOutputFilter DEFLATE

    I am not getting an error. It is just not working with the css and js according to yslow. I was able to set a php.ini file to gzip the php. Although, I am not sure how to do the same thing for the css/js.

  • Prasannah

    I don't see any changes in the page load speed AT ALL! I used this tool (Pingdom) to test my speeds. Any thoughts?

  • http://www.askapache.com/ AskApache

    @ Jason

    You aren't using a FilesMatch but other than that I couldn't tell you, it depends on your server, configuration, and modules.

    @ Prasannah

    This really won't make that much of a difference to begin with but you can use the http header viewer tool by requesting directly your .css or .js file with this .htaccess compression code on and then with this code off and you should see a difference unless something on your end isn't working correctly.

  • http://blog.luga.name lu-blog

    Will the server in this case send uncompressed content to client if the client didn't send deflate in HTTP_ACCEPT_ENCODING?

  • http://www.askapache.com/ AskApache

    Yes. You can test my site out using the http headers tool. Just put "none" in the accept-encoding field.

    Though actually, I am using gzip for compression and I am actually serving both XTHML 1.1 with the application/xhtml+xml content-type, and plain text/html for microsoft browsers that can't handle it.

    I do have mod_deflate turned on for my javascript and css files though.

  • http://alicious.com/ alicious

    I normally add php|html to my match for zlib compression, any reason why you haven't? Is deflate better than zlib for some reason. Also with javascript if you've got minified files I imagine it's not going to help much (if at all).

  • http://jeeshenlee.wordpress.com/ JeeShen Lee

    Hello,

    For ASP user like me, Gzip compression can be turn on using web.config. Refer to my blog post below for more information.
    "How to Gzip on ASP.NET"

    I have tested it on GoDaddy Window Shared hosting and it works.

    Regards,
    JeeShen Lee.

  • Jazzer

    if you get 500 internal error, add these to your httpd.conf

    LoadModule expires_module /usr/lib/apache2/modules/mod_expires.so
    LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
  • http://travis-larsen.com Travis

    Awesome Insight. Thank you!

    Just wondering, though... why aren't you using <!-- -->

    Wouldn't that make you site even faster still? I am using it unnecessarily?

    • http://killerdomains.com.au frank

      Thanks for the help!

      There is a ton on the net, and I found this to be the best so far.

      Not too complex, and now I can add in the code and enjoy a faster site!!!

      It used to be soooo slow. Not anymore! :)

  • http://www.askapache.com/ AskApache

    @JeeShen Lee

    Thanks for that excellent addition! I hope you move away from ASP ASAP however. Enjoyed browsing your blog too, thanks for your research.

  • http://www.driver-driver-driver.com jojo

    i have huge xml files in my site.
    can i use the gzip on xml files?
    does that make any difference?

    p.s
    great post
    thanks

  • http://www.ameeto.com Ameeto
    SetOutputFilter DEFLATE

    also works good..!!
    well thanks for this
    IF you want to gzip every page of your site you can also use this

    php_value output_handler ob_gzhandler

    but be make sure this sometimes give error or some modules will not work..!!
    But if your website is static not conatin only blog and images
    php_value output_handler ob_gzhandler
    works great..!!

  • Merino Renato

    Hi everybody:

    I recently used the google page speed service and i end up in this grat website, which offers must of the information i was looking for, i just a rookie in web pages and apache stuff, now that i find your website i got a lotta more things to read, but i have a question in the gzip code you post for httaccess:

    SetOutputFilter DEFLATE

    I cant see the Gzip name... so this works for gzip compression cause i heard theres two type of them, deflate and the other one is gzip compression, so will this code will work for my google page speed improvement, and is there a particular place i shoul paste the code in the httaccess file? i mean before of after the rewrite cond or is it the same where i paste it?

    Well, thanks for creating this website, i didnt knew there were so many ways to optimize our apache control panel, oh i almost forget to ask, does any of this "improvements" causes any problem with shae hosting?
    I mean if i over optimize my apache server...

    Thanks again

  • http://interviewpattern.com Interview

    Great post, another optimization is to disable .htaccess files or apache will try to look it in all folders on each request.

  • http://URL stosun

    Hi,
    I try to use compression, but I couldn't success it.
    I use gkg.net as hosting and I enabled compression at cpanel for text/html, text/ccs and application/javascript.
    I put a .htaccess file to main directory.
    Content of .htaccess file is;

    AddOutPutFilterByType DEFLATE text/html text/plain text/xml application/javascript text/css
     
    # 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

    gkg server is Apache, client borowser is chrome and Firefox.
    At pagespeed of firefox or chrome , I couldn't see content-encoding:gzip as response headers, whereas browser requests it as accept-encoding:gzip,deflate.

    gkg.net says that gzip is enable but I couldn't see it.
    Is it needed to add some more code .htaccess file?
    Any help aprreciated, thanks at advance.
    Regards

  • http://www.apnatimepass.com Varun sharma

    how to know if i do have access to . .htaccess file. I use window shared hosting on godaddy server.
    Could you tell me if I have access to this file

  • Pingback: Tweak .htaccess, speed up your wordpress without any plugin | smokincoffee

  • Pingback: How do I gzip my web files - PHP Solutions - Developers Q & A

My Online Tools
WordPress Sites

My Picks

Related Articles
Newest Posts
Twitter

  • The Hacker Playbook - very nice high level overview of attacks  t.co/lHwNVWi61u 
  • Clean Code - A Handbook of Agile Software Craftsmanship  t.co/hnJX0x1qIc 
  • Secrets of the JavaScript Ninja - By my absolute favorite JS hacker John Resig!  t.co/tZ42ljmcCl 
  • Hacking Exposed 7: Network Security Secrets & SolutionsMy all time favorite, basic but thorough and accurate.  t.co/jycW0RDVtZ 
  • Empty words will be no surrogate for cold resolve. Pain is nothing.  t.co/qXjpRxbjCw 
  • REVERSING: Secrets of Reverse Engineering  t.co/GaWo29lWWG 
  • NEUROMANCER  t.co/3OoknUcb5Z 
  • "The Shockwave Rider", by John Brunner (1975 hacker sci-fi)  t.co/ZW56HVUefW 
  • The Rootkit ARSENAL - Escape and Evasion in the Dark Corners of the System  t.co/1FzX6bHgsQ 
  • "We Are Anonymous - Inside the Hacker World of LulzSec, Anonymous, and the Global Cyber Insurgency" better be good!  t.co/GL0cFNiUOq 
  • THE IDEA FACTORY Bell Labs  t.co/FyVhgNwwT5 
  • The Datacenter as a Computer -- Urs Holzle  t.co/M5WIYs1OVg 
  • Now by Steven Levy, "IN THE PLEX"  t.co/PwxtLgqukG 
  • Dreaming in code.... So far, a little boring, but worth the read  t.co/hmeeOjIlfg 

Friends and Recommends
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+ | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain