[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

enable gzip compression via .htaccess

Posted on 2011-05-10
14
Medium Priority
?
969 Views
Last Modified: 2013-12-25
Hi,

I've search the internet for a while now, and i can't get the compression working for my website.
Or my hosting provider doesn't support it, or i'm using the wrong code, or i'm doing it wrong.

results on gzip from phpinfo():
_SERVER["HTTP_ACCEPT_ENCODING"]      
gzip,deflate,sdch
HTTP_ACCEPT_ENCODING
     
gzip,deflate,sdch
Accept-Encoding
     
gzip,deflate,sdch

code i tried in .htaccess (2x):

 
# BEGIN GZIP
#<ifmodule mod_deflate.c>
#AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-httpd-php application/x-javascript application/javascript
#</ifmodule>
# END GZIP


<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  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.*
</ifModule>

Open in new window


Thanks!
0
Comment
Question by:peps03
  • 7
  • 4
  • 3
14 Comments
 
LVL 8

Expert Comment

by:DocSeltsam
ID: 35727323
Hi there,

maybe you are running on Apche 2?

Try this...

 
<IfModule mod_deflate.c>
  <FilesMatch "\.(html?|txt|css|js|php|pl)$">
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>

Open in new window



If you can use php on your server and do not have console access, try this to get a list of modules

http://www.phpbuilder.com/manual/function.apache-get-modules.php

--TheDoctor
0
 

Author Comment

by:peps03
ID: 35727336
From phpinfo():
Server API      Apache 2.0 Handler

so i think you are right. but it doesn't work..
0
 
LVL 8

Expert Comment

by:DocSeltsam
ID: 35727567
Did you try the procedure described behind the link to get the installed modules?

--TheDoctor
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:peps03
ID: 35727586
excuse me, didn't see that.
it returns:

Array ( [0] => core [1] => mod_log_config [2] => mod_logio [3] => itk [4] => http_core [5] => mod_so [6] => mod_actions [7] => mod_alias [8] => mod_auth_basic [9] => mod_auth_plain [10] => mod_authn_file [11] => mod_authz_default [12] => mod_authz_host [13] => mod_authz_user [14] => mod_autoindex [15] => mod_cgi [16] => mod_dir [17] => mod_env [18] => mod_expires [19] => mod_include [20] => mod_info [21] => mod_mime [22] => mod_mime_magic [23] => mod_reservefd [24] => mod_negotiation [25] => mod_php5 [26] => mod_rewrite [27] => mod_setenvif [28] => mod_ssl [29] => mod_status [30] => mod_suexec )
0
 
LVL 8

Expert Comment

by:DocSeltsam
ID: 35727812
Hmm, seems you're out of luck since the modules are missing.

Can you check your provider if adding the mod is an option?

Alternatively, if youre building a php site, have a look here:

http://www.webcodingtech.com/php/gzip-compression.php

--TheDoctor
0
 

Author Comment

by:peps03
ID: 35727835
2 bad for me..

yes, i am building a php site. does your script also compress css and js scripts that are include in the site like this:
<script src="./scripts/jquery.min.js" type="text/javascript"></script>
0
 
LVL 4

Expert Comment

by:a1j
ID: 35734256

#
# mod_deflate options
#
# WARNING mime type here is type from extension,
# not from actual content type header
#
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css text/javascript application/x-javascript

SetEnvIfNoCase Request_URI ^/simeURLyouwanttoexclude/ no-gzip !dont-vary

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|swf)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
#
DeflateCompressionLevel 3
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

#
0
 
LVL 4

Expert Comment

by:a1j
ID: 35734263
Note - use mod_deflate, do NOT use mod_gzip. It is outdated and not maintained.
0
 

Author Comment

by:peps03
ID: 35736061
Thanks a1j!

Do i only have to add this to my .htaccess? nothing else? Leave the rest of my page like it is?

what should i do with this line:
SetEnvIfNoCase Request_URI ^/simeURLyouwanttoexclude/ no-gzip !dont-vary

Thanks!!
0
 
LVL 8

Expert Comment

by:DocSeltsam
ID: 35737503
Hi again peps03,

if you want to compress any static content like css and js, you need to write a little
php-script based on the code on the linked webpage which gets the requested content, eg. main.css nad compresses it.

Then, with a mod_rewrite line in yout .htaccess, you can map a request on the /css location to that script.

--TheDoctor
0
 

Author Comment

by:peps03
ID: 35737675
Hi Doc!

I now compressed the files manually as my hosting provider doesn't support that function.

I think i'm already using the script your talking about here: http://www.experts-exchange.com/Programming/Languages/Scripting/CGI/Q_27031447.html

but i'm having issues with that as you can read there.

Is this script what you meant?

Thanks!
0
 
LVL 4

Expert Comment

by:a1j
ID: 35738611
I dont know if it will work in .htaccess, you can try though.

You can delete that line, i added it as example.
You also have to add content type of your php files if you want to compress output from them.
0
 

Accepted Solution

by:
peps03 earned 0 total points
ID: 35741972
Hi guys!

I finally got it all fixed..
Because my server @ my hosting provider doesn't allow server compression i compressed the files manually. (with 7zip)
after compressing change .gz to .jgz
I uploaded hem to the same folder as the originals. don't change anything in your html code.
and add the below text to the .htaccess.
you can even exclude folders ;)

pffew.
###############GZIP##############
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
##JS
RewriteRule (.*)\.js$ $1\.js.jgz 
AddType "text/javascript" .js.jgz

##EXCLUDE CMS FOLDER
RewriteCond %{REQUEST_URI} !^/cms/ [NC]

##CSS
RewriteRule (.*)\.css$ $1\.css.jgz
AddType "text/css" .css

##OUTPUT
AddEncoding gzip .jgz [L]
###############GZIP END###########

Open in new window

0
 

Author Closing Comment

by:peps03
ID: 37175764
see last post
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question