Technology, life, programming, travel and money.

Compressing HTML and PHP files on 1and1

1and1 doesn’t have mod_deflate or mod_gzip enabled on its home or business shared server packages which means that many of the usual .htaccess tweaks to compress your web pages won’t work. Here’s something very simple tweaks that will work with 1and1, and with other hosts too.

Set up your .htm and .html files so that they are parsed by the PHP interpreter by adding this one line to your .htaccess file.

AddType x-mapp-php5 .html .htm

Then create and upload a php.ini file to your websites root directory. If you are using WordPress then this should be the same directory as the one where wp-config.php is.

The contents of the php.ini should be as follows.

zlib.output_compression = On

This line causes PHP files to be served as compressed if the browser supports it. And as we have registered .html and .htm as being PHP files they will now be compressed.

You can test that the HTTP compression is turned on by using this simple HTTP compression checking tool, or by using the more comprehensive Page Speed add-on for Firefox from Google.

Here are the results from the HTTP compression checking tool showing that compression is now on, and that the home page HTML is being served using 76% less data. Note that this saving refers only to the HTML page, and not to any other images, style sheets, or JavaScript.

rml gzipped on 1and1

Increasing expiry time of images on 1and1

Another quick tweak you can make to your .htaccess is to increase the expiry time of your images, and other data files. This will result in quicker loading times for regular visitors as the files will stay in their browsers cache for longer.

Add this to your .htaccess. You can increase the expiry times if you don’t think those files will change.

#Increase cache time for resources

ExpiresActive On
ExpiresByType image/gif "access plus 2 weeks"
ExpiresByType image/jpeg "access plus 2 weeks"
ExpiresByType image/png "access plus 2 weeks"
ExpiresByType image/ico "access plus 2 weeks"
ExpiresByType text/css "access plus 2 weeks"
ExpiresByType text/javascript "access plus 2 weeks"
ExpiresByType application/x-javascript "access plus 2 weeks"

Use Google’s Page Speed add-on to verify that this has worked. Go to the Resources tab to see the headers for each individual file served from the HTTP server. Below you can see the current date/time, and that the expiry time is set two weeks into the future.

expires date for image

Caching and expiry changes combined

Here are before and after results for Google’s Page Speed tool of applying both the above changes to another of my websites. You can see that these quick changes give the website a small increase in score.

website score before

From 88 to 92.

website score after

This isn’t a big jump, but it may make a difference. I highly recommend you look at the other performance recommendations from the Page Speed tool, as it can give you loads of ideas for how to make your site faster and more responsive.

Reader Feedback

9 Responses to “Compressing HTML and PHP files on 1and1”

  1. nicolas1106 says:

    What about adding the following to .htaccess file in order to have the .css and .js compressed as well ?
    AddType x-mapp-php5 .php .shtml .html .htm .js .css .txt
    Will it work ?
    Good article by the way.

  2. advancedhtml says:

    Hi Nicolas, that is an interesting idea. In theory it may work and may get these files compressed. It may however confuse your browser when it requests a .css file and gets a file of a mismatching content type. I’m not sure though. You’d have to try it to find out what happens!

  3. advancedhtml says:

    By the way – if you are using WordPress you can use the WP Minify plugin to compress and minify the CSS and JavaScript.

  4. Sandra says:

    Thanks for this article. After having dealt with the clueless 1&1 support (“what exactly do you want do compress? and where again?”) I finally found the answer here. Works like a charm.

  5. Onur says:

    Thank you very much indeed. I tried many ways to compress (on 1and1 servers) for 2 days and now thanks to you, in 1 minute it works !

  6. Sean says:

    I have been fighting with gzip for a couple of years now. Finally I got it working, thanks to you.

  7. GGecko says:

    Thanks! This worked for me after hours of trying (and failing) to get gzip to work via htaccess on 1&1. Great solution!

  8. Gigi says:

    I should add that as the new version of PHP as 5.5 so I had the automatic line generated by 1and1 in the .htaccess as:

    AddHandler x-mapp-php5.5 .php .htm .html .js

    of course the .php was the only one but you should add .htm and .html and .js

  9. Paul T says:

    Can you tell me if you are able to compress css + javascript files with the same method. Thank you. Paul

Leave a Reply

Do NOT fill this !