FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Htaccess » Speed Tips: Turn On Compression

Speed Tips: Turn On Compression

Speed Tips: Turn On Compression Compress files before sending to the client using mod_deflate.

Speed Tips: Turn On Compression

September 5th, 2007

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!

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

#####################################################
# CONFIGURE media caching
#
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

Easy Apache Speed Tips Articles

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>

http://www.askapache.com/htaccess/apache-speed-compression.html#comments

Reader Comments

  1. Merino Renato ~September 21, 2011 @ 5:47 am
    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
  2. Ameeto ~June 20, 2011 @ 12:44 am
    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..!!
  3. frank ~May 26, 2011 @ 11:08 am
    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! :)
  4. jojo ~May 7, 2011 @ 6:53 pm
    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
  5. AskApache ~October 30, 2010 @ 4:57 pm

    @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.

  6. Travis ~October 15, 2010 @ 2:01 am
    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?
  7. Jazzer ~October 11, 2010 @ 10:11 am
    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
  8. JeeShen Lee ~October 6, 2010 @ 5:09 am
    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.
  9. Piers ~June 15, 2010 @ 1:51 am
    Dont know how old these comment are but I encountered the same problem with the 500 error when I tried to gzip css & js files. I found this site - http://www.speedingupwebsite.com/2010/01/09/static-gzip-or-compression-without-compression/ - and it works! Basically you update the htaccess file with the code, manually zip all the files and upload them all. Hope it works for you :)
  10. [d] ~June 14, 2010 @ 7:31 pm
    http://code.google.com/speed/page-speed/download.html has similar functionality and much more. But this is a good off-line help.
  11. u64 ~May 26, 2010 @ 7:45 pm
    Files that also compress nicely
    1. txt
    2. mid
    3. svg
  12. Thomas Horster ~April 2, 2010 @ 1:43 pm

    Another question:

    What happens if I change one of the images?

    For example if I use css sprites? Do I have to version them?

    Does for example header.png?ver=123 work as a css background if the file has been changed?

    :D
  13. Thomas Horster ~April 2, 2010 @ 1:32 pm
    Hey there, I've got a question. Why do you turn off etags for everything but in http://www.askapache.com/htaccess/apache-speed-etags.html you say, that we should not turn them off for html files? Am I missing something? Regards, Tom
  14. desi ~March 3, 2010 @ 10:12 am
    thanks :) I tried it ..and works flawlessly :)
  15. Royads ~October 28, 2009 @ 6:37 am
    Hey Thanks for sharing the information. I am using the same technique on one of my website to get benefits of compression.
    Header set Expires "Mon, 20 Apr 2012 23:30:00 GMT"
    Header append Cache-Control "public"
     
    I am writing this in .htaccess file in my website root. But when I am testing my website with google page speed it gives me this message. Compressing the following resources with gzip could reduce their transfer size by about two thirds (~53.1kB). List of all my css and JS files, but not the images file. So I have doubt whether it work for CSS and js or not. Because its working for my images contents. Can any one explain. Thanks a lot in advance. Roy
  16. Fasst ~September 21, 2009 @ 6:36 am
    very helpful speed tips bud... thanks
  17. alicious ~February 28, 2009 @ 7:48 pm
    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).
  18. AskApache ~July 19, 2008 @ 2:13 am
    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.
  19. lu-blog ~July 19, 2008 @ 1:38 am
    Will the server in this case send uncompressed content to client if the client didn't send deflate in HTTP_ACCEPT_ENCODING?
  20. AskApache ~May 31, 2008 @ 11:09 pm

    @ 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.

  21. Prasannah ~May 18, 2008 @ 1:25 pm
    I don't see any changes in the page load speed AT ALL! I used this tool (Pingdom) to test my speeds. Any thoughts?
  22. Jason ~April 19, 2008 @ 6:31 am
    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
    &nbsp;
    #12 Months
    &nbsp;
    Header set Cache-Control "max-age=29030400, public"
    &nbsp;
    Header unset ETag
    FileETag None
    &nbsp;
    # 2 HOURS
    &nbsp;
    Header set Cache-Control "max-age=7200, must-revalidate"
    &nbsp;
    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.
  23. AskApache ~April 19, 2008 @ 12:07 am

    @ 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.

  24. Jason ~April 18, 2008 @ 8:48 am
    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.
  25. AskApache ~April 8, 2008 @ 11:35 am

    @ 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>
  26. Jason ~April 7, 2008 @ 8:59 pm
    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"
    &nbsp;
    Header unset ETag
    FileETag None
    &nbsp;
    SetOutputFilter MOD_DEFLATE
  27. Isaiah ~November 9, 2007 @ 9:42 am
    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
  28. Paul ~November 6, 2007 @ 8:31 am
    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?
  29. Ajay ~September 25, 2007 @ 3:07 am
    My site throws up a 500 error when I do this. Any suggestions?

Add Comment!

Leave a Reply

Your email address will not be published.


Google +

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.

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

↑ TOPMain