« Downloading Multiple Files with Curl SimultaneouslyWget Trick to Download from Restrictive Sites »
Speed Tips: Turn On Compression
September 5th, 2007
Contents
Apache 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 On Compression
- Add Future Expires Header
- Add Cache-Control Headers
- Turn Off ETags
- Remove Last-Modified Header
- Use Multiple SubDomains
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
With 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>
Reader Comments
-
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
-
SetOutputFilter DEFLATE
also works good..!! well thanks for this IF you want to gzip every page of your site you can also use thisphp_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 imagesphp_value output_handler ob_gzhandlerworks great..!! -
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! :)
-
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
-
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?
-
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
-
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.
-
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 :)
-
http://code.google.com/speed/page-speed/download.html has similar functionality and much more. But this is a good off-line help.
-
Files that also compress nicely
- txt
- mid
- svg
-
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
:Dheader.png?ver=123work as a css background if the file has been changed? -
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
-
thanks :) I tried it ..and works flawlessly :)
-
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 -
very helpful speed tips bud... thanks
-
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).
-
Will the server in this case send uncompressed content to client if the client didn't send deflate in HTTP_ACCEPT_ENCODING?
-
I don't see any changes in the page load speed AT ALL! I used this tool (Pingdom) to test my speeds. Any thoughts?
-
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 DEFLATEI 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. -
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.
-
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
-
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 -
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?
-
My site throws up a 500 error when I do this. Any suggestions?
