• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

How Do you Write Your Header with GZip

How do you write a link in the head of a document as a GZip file?

Specifically your CSS files/
  • 5
  • 4
1 Solution
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.*

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

Open in new window

rgranlundAuthor Commented:
I'm sorry, I should have been more clear:  How do you write it in the HEAD of your website document?
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:
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rgranlundAuthor Commented:
Yes, YSlow indicates that I need to gzip certain files in the head of my document.  I am not sure how to do that.
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').
rgranlundAuthor Commented:
Ohh!  I understand now!  Thank you for taking the time to explain.
notice I made a typo in my last remark: it is ".htaccess"
rgranlundAuthor Commented:
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
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

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now