Solved

How Do you Write Your Header with GZip

Posted on 2012-04-02
9
183 Views
Last Modified: 2012-04-06
How do you write a link in the head of a document as a GZip file?

Specifically your CSS files/
0
Comment
Question by:rgranlund
  • 5
  • 4
9 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37798564
I assume what you want is for your server to compress the data it send to the users' browsers.
For this, you will have to enable gzip compression in your server.
How this is done varies from server to server, and from server version to server version.
This is what you place in the .htaccess file in the server folder you want to active, in case you are using Apache 1.3:
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php)$
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


And this is for Apache 2:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
# Don’t compress
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
#Dealing with proxy servers
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>
</IfModule>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 37798787
I'm sorry, I should have been more clear:  How do you write it in the HEAD of your website document?
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37800171
You don't.
HTML is text that reaches the browser, which in turn parses it.
Compression is provided by the server, as per my answer above.
You can check this faq from Yahoo. It gives you a simple explanation and more hints on how to speed up your site:
http://developer.yahoo.com/performance/rules.html
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:rgranlund
ID: 37802018
Yes, YSlow indicates that I need to gzip certain files in the head of my document.  I am not sure how to do that.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37802427
You're confusing the <head> element of an HTML page with the HTTP response headers sent from the server.
If your server is Apache, then take the proper piece of code I posted above and stick it inside an .htacces file in your document root (the folder named 'public_html', or 'http_docs').
0
 
LVL 7

Author Comment

by:rgranlund
ID: 37802494
Ohh!  I understand now!  Thank you for taking the time to explain.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37802507
notice I made a typo in my last remark: it is ".htaccess"
0
 
LVL 7

Author Comment

by:rgranlund
ID: 37802598
Can you use both in your .htaccess?

OR what about the following:
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37802712
This is a more thorough solution, courtesy of Apache manual (http://httpd.apache.org/docs/2.0/mod/mod_deflate.html):

<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

# 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
</Location>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

785 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