Gzip with 1and1

When creating a WordPress site recently to make it load super fast I wanted to make the site served the content using compressed gzip files.¬† Not a problem I thought it uses apache so let’s just stick this in the .htaccess file:

# 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

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE

But after checking and banging my head on the table and scratching nearly all my hair out it would just never ever work! WWHHYYYY???

It turns out the if you are hosting through 1and1 which I was then they do not support  DEFLATE so as you can imagine it is a little more tricky. Many people I spoke to simply said it was not possible and I would just have to give up on the idea, but it turns out they were wrong, What you need to do is add something a bit different to your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteOptions Inherit
ReWriteCond %{HTTP:accept-encoding} (gzip.*) 
ReWriteCond %{REQUEST_FILENAME} !.+\.gz$ 
RewriteCond %{REQUEST_FILENAME}.gz -f 
RewriteRule (.+) $1.gz [QSA,L] 

But there is another catch to this solution, you have to gzip the js, css and image files (if you choose to serve all of them compressed) yourself. The other issue is if you want to serve compressed dynamically created html files too. To do that you need to create a php.ini file in the root of your site and put this in there:

zlib.output_compression =1
zlib.output_compression_level =9

Of course the compression level is up to you 9 is the highest so you can adjust as you see fit. That seemed to work for me fine but another answer I stumbled across that might be of interest is a slight play on the one above:

zlib.output_compression = On
zlib.output_compression_level = 9

Then add this to your .htaccess as well:

AddType x-mapp-php6 .html .htm .php

Apparently although that is php6 it will run fine on php5.4 and will allow gzip compression this means it will run html and htm files through the php parser as well and you can add other extensions. Just in case that helps you out any further. My first option seems to work just fine for me but you know me I like to give you guys options.

One thought on “Gzip with 1and1

  1. Lee


    I have been trying to achieve this for days, driving myself nuts just as you did and as if by magic your code works!! Life saver, thanks so so much!!


Leave a Reply

Your email address will not be published. Required fields are marked *