Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We should not gzip images, right? How do I avoid gzipping on images like this: img/sample.php?id=image_name.jpg can also be called like this img/sample.php?id=image_name.jpg&size=3 the actual images live here /images/items/

I tried using 2 types of configuration for gzip in httpd.conf (see below), but in both cases the images were gzipped anyway.

Images are obviously not being treated like a regular .jpg file, because if it was it would not have been gzipped with any of the below configurations. However live headers shows it as a regular image/jpeg

Any idea how to fix this?

1st try:

<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
<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>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>

2nd try:

    <IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain    
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/xml    
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/json
</IfModule>
share|improve this question

This part:

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

Is looking for image file extensions in the request URI. Your file extensions are in the query string.

Unfortunately, mod_setenvif doesn't have access to the query string and if I am reading the documentation correctly, neither does LocationMatch.

mod_rewrite, however, does have access to the query string and can set environment variables.

RewriteCond %{QUERY_STRING} \.(?:gif|jpe?g|png)$
RewriteRule ^ - [E=no-gzip,dont-vary] 

If you're using mod_rewrite anyway, it might be a better idea to use it to rewrite the URLs so that they don't have a query string at all. The following makes the above two lines unnecessary.

RewriteRule /generated_images/(.*\.(?:gif|jpe?g|png))$ /sample.php?id=$1
share|improve this answer
    
Thanks @Ladadadada for the detailed instructions and clarification! will your last line of code redirect the php images to new url? or I'll have to call it from the static location? and "generated_images" is an example only, right? – Joel Mar 6 '12 at 17:56
    
There's no redirecting here, only rewriting. This means the URL will stay as the browser originally requested it. You can make it a redirect by using [R] at the end. Yes, generated_images is an example. You can call it whatever you want. – Ladadadada Mar 6 '12 at 17:59
    
I did what you said to rewrite the query image uri, but it still shows as being gzipped:( see whatsmyip.org/http-compression-test/… vs the regular image file which is not gzipped whatsmyip.org/http-compression-test/… (it was done in php, is this the problem? most be done in apache?) – Joel Apr 25 '12 at 16:08

try including this:

<LocationMatch \.jpg$>
  SetOutputFilter none
</LocationMatch>
share|improve this answer
    
Thanks @pQd, can this be used with both of my codes? does it matter where I add it, below or above the deflate code? – Joel Mar 4 '12 at 21:39
    
just check it empirically. – pQd Mar 5 '12 at 6:22
    
sorry for the dumb question.. how do I do it with an entire directory <LocationMatch dir_name/$>? – Joel Mar 5 '12 at 21:11
1  
<Location > is not for directories; it matches URI paths whether or not they correspond to actual directories in the filesystem. Use <Directory /var/www/whatever/dir_name> for directories. – Ladadadada Mar 5 '12 at 21:19

I'm using this in the .htaccess file which is simple enough:

<FilesMatch "\.(gif|jpg|png|zip|7z|rar|mp3|avi|swf)$">
    SetEnv no-gzip 1
    SetEnv dont-vary 1
</FilesMatch>

And for image files sent with scripts and a querystring, you add this:

RewriteEngine on

RewriteCond %{QUERY_STRING} gif|jpg|png [NC]

RewriteRule ^ - [E=no-gzip,dont-vary]

Note: These are case sensitive, so if the file was named IMAGE.JPG, the server would still intervene and gzip them. Add uppercase extensions if you need them. I didn't use the '$' as you appended a size variable to your querystring and so you need a substring search, not end of string.

Also, it would be better to choose a unique name to the script that handles image files like "image.php" or use a unique variable in the query string to trap and not worry about every image extension.

E.g. RewriteCond %{QUERY_STRING} op=download_image [NC]

RewriteRule ^ - [E=no-gzip,dont-vary]

For the case of say /sample.php?op=download_image&id=image_name.jpg&size=3

Trapping "image.php" would be shorter of course with the request string, but either way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.