Photo of source code from this site

Setting Up Gzip on 1&1 Hosting

Posted on:
December 1, 2015
Posted in:
JavaScript, Programming, Web Dev, WordPress

It took a bit of googling but I finally figured out how to enable gzip compression for 1&1 accounts: buy a pro account. That’s the official answer anyway. The basic hosting packages run on Apache servers that don’t have mod_deflate installed, so tough cookies. There’s a way around it, though, if you’re willing to put in a bit of time.

Step 1: Enable gzip for PHP

PHP has its own module for gzip compression that can be accessed through zlib. When you request a PHP page from an Apache server, Apache basically hands everything over to PHP which can handle its own compression.

To enable this on a basic 1&1 account, create a file in your root directory called php.ini and put the following text in it:

zlib.output_compression =1
zlib.output_compression_level =9

Presto! Your PHP pages will now be gzip compressed.

Step2: Create a PHP Compressor

This is a bit of a misnomer since the PHP script isn’t doing any compressing. Since gzip is enabled for PHP pages you only need the script to echo text content.

Create a new file in your root directory called css-js-compressor.php and put in the following code:


// part 1
$key = "REDIRECT_URL";
if (isset($_SERVER["SCRIPT_URL"])) {
	$key = "SCRIPT_URL";

// part 2
if (preg_match("/\\.css$/i", $_SERVER[$key])) {
	$ContentType = "Content-type: text/css; charset=utf-8";
} else if (preg_match("/\\.js$/i", $_SERVER[$key])) {
	$ContentType = "Content-type: text/javascript; charset=utf-8";
} else {
	exit("no match");

// part 3
header("Cache-control: max-age=604800, public");
echo ("/* compressor */\n");
echo file_get_contents($_SERVER["DOCUMENT_ROOT"].$_SERVER[$key]);

Part 1: Different versions of Apache structure the $_SERVER variable differently. This checks to see which version is going to give us the requested filename (without any querystring).

Part 2: Make sure that the requested file ends in .css or .js and use that to select the appropriate content type. If it’s not CSS or JavaScript, exit.

Part 3: First set the content type header and a cache control header. This one makes my CSS and JavaScript files valid for up to 7 days and allows them to be cached downstream (by, for example, your ISP). I insert a comment just so that I know which CSS and JavaScript files were passed through this script, and then echo out the contents of the file.

Step 3: Redirect requests for CSS and JavaScript to PHP

If you don’t already have one, make a text file in your root directory called .htaccess and paste in the following code:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(wp-content|wp-includes)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js)$ /css-js-compressor.php [L]


There’s a bit going on here if you’re not familiar with Apache’s mod_rewrite so let me unpack it.

Lines 3-5: If the module is installed, turn it on and set our base to the root of the site.

Line 6: If the requested file is in either the wp-content or wp-includes directory. (I’m running a WordPress site, so all my CSS and JS should be in there. You can change or remove this line if you store your files elsewhere.)

Line 7: If the requested file is an actual file.

Line 8: Change any URLs that end in .css or .js to /css-js-compressor.php (but, because I use option [L] instead of [LR], the user will not see the filename change).

It’s important if you edit this to understand how Apache handles RewriteCond and RewriteRule. Basically it’ll chain together all of the RewriteCond statements immediately before a RewriteRule and if all of them are true then it’ll execute the rule.

You are allowed to specify OR instead of AND for RewriteCond, but in this script it’s all AND.


To check your results, open your site in Firefox with the network monitor open. The “Transferred” sizes for your CSS and JavaScript files should be less than the “Size” sizes. You should also be able to calculate how much bandwidth you’ve saved.

Results from this site. Size: 267.47KB. Transferred: 144.63KB. Reduction: 45.9%.

A final note of caution: The most likely reason 1&1 doesn’t install mod_deflate on basic web hosting accounts is that they’re concerned that the low-cost (and likely lower powered) shared servers won’t be able to handle the increased load. That’s why it’s critical to make sure that you set generous cache control parameters to help avoid that computational load whenever possible.

Each visitor who comes to your site should only be requesting each CSS and JS file from Apache exactly once – and even then public caching will allow ISPs to fulfill that request for hundreds of visitors.