GZIP compression + htaccess deflate
Asked Answered
O

1

9

Can I have both .htaccess with:

  DEFLATE 

On php, images, html files etc. + php header with:

  ob_start("gzhandler") ?

If no, what is the best opportunity? I am just worried if it does conflict.

Omnivore answered 22/12, 2012 at 19:14 Comment(0)
T
30

Using compression on images is usually a pretty bad idea since most of the widely used image formats on the web are already compressed so you would be only adding unnecessary overhead to the files. You generally want to use compression on resources that are textual in nature (HTML, CSS, JavaScript etc.) because for those the compression ratio is extremely high.

As for the question itself as far as I know it is not possible to use both DEFLATE and GZIP at the same time but honestly since I was never in a situation to try out something like that please bear with me if this information is incorrect.

As to which one to choose I would strongly recommend to take a look at the following post where you can see some of the pros and cons of both DEFLATE and GZIP.

Why use deflate instead of gzip for text files served by Apache?

I personally use DEFLATE whenever possible simply because its sometimes easier to implement through .htaccess than poking around the code. I also like the possibility to quickly disable that functionality when testing or developing stuff.

Apache Server Configs project has a pretty comprehensive .htaccess file so you might want to check the project out HERE.

Now although that file is pretty comprehensive you might just want to use a normal case scenario configuration like the following one:

# -----------------------------------------------------------------------
# Defining MIME types to ensure the web server actually knows about them.
# -----------------------------------------------------------------------
<IfModule mod_mime.c>
    AddType application/javascript          js
    AddType application/vnd.ms-fontobject   eot
    AddType application/x-font-ttf          ttf ttc
    AddType font/opentype                   otf
    AddType application/x-font-woff         woff
    AddType image/svg+xml                   svg svgz 
    AddEncoding gzip                        svgz
</Ifmodule>

# -----------------------------------------------------------------------
# Compressing output.
# -----------------------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</Ifmodule>
Trutko answered 22/12, 2012 at 19:25 Comment(5)
Ok thanks, will this be good? ExpiresActive on <FilesMatch "\.(ico|pdf|flv|js|css|swf|class|php|jar|bmp|css|woff|ttf|svg|eot)$"> ExpiresDefault A2592000 Header unset Cache-Control SetOutputFilter DEFLATE </FilesMatch>Omnivore
That code has nothing to do with compression. Please take a look at my updated answer for some of the "best practices".Trutko
This works for me, BUT the line AddOutputFilterByType DEFLATE text/xml application/xml text/x-component produced a big error on Windows 7 (all browsers), where the Website could not be delivered at all (cPanel, TYPO3 with URL rewriting). I commented it out.Grease
It looks like the HTML5 Boilerplate link is broken. Here's a working link, but do I have the correct file?: github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccessDegrading
@thohl Yes that's the correct link. The only difference is that the .htaccess is now using a somewhat different syntax. I updated my answer.Trutko

© 2022 - 2024 — McMap. All rights reserved.