Sunday, August 2, 2009

Godaddy GZIP compression

I have a GoDaddy hosting and domain (mycila.com) and created a sub-domain res.mycila.com where i put all my resources (javascripts, images, ...).
So all the MooTools scripts are there, and i wanted to use gzip to serve them, as it reduces javascript sizes by two or more.
The main issue is that Godaddy uses Apache but without mod_gzip or mod_deflate. So by reading these articles on gzip compression and this Drupal use case, i finally found a solution that works.

Just put a .htaccess file in the root of the subdomain, with the content:

rewriteengine on
rewritebase /
rewritecond %{HTTP_USER_AGENT} !".*Safari.*"
rewritecond %{HTTP:Accept-encoding} gzip
rewritecond %{REQUEST_FILENAME}.gz -f
rewriterule (.*) $1.gz [L,QSA]

AddEncoding x-gzip .gz
Header set Content-Encoding: gzip
ForceType text/javascript


Then, simply login with SSH and execute this script to compress all your existing javascripts:

#!/bin/bash
cd "/your/base/folder/for/resources"
for i in `find . -name *.js`
do
echo "Compressing $i..."
gzip -c -9 $i > $i.gz
done

If it is to complicated for you, just always upload the compressed file. In example, when i upload my mycila.js file, i also upload the compressed mycila.js.gz file also.

19 comments:

  1. Godaddy Domain name said in this article Nice..I found the Domain name registration site it is the reseller of godaddy.com the site is tucktail.com..
    ReplyDelete
  2. Hi

    I host one domain on Godaddy too. I use Wordpress and not so sure I can do this type of changing .htaccess to speed things up.

    I googling and found this help on Godaddy resources center http://help.godaddy.com/article/4485

    It's a good idea to host images in another sub domain. I'll start to do that also.

    thanks
    KOmgrit
    ReplyDelete
  3. hi, how do you know it is working?
    i did the same setting on godaddy and I cannot tell.

    thanks cordoval @ gmail dot com
    ReplyDelete
  4. Just type 'gzip test' in Google and you'll see test websites. Otherwise use Firebug's Firefox plugin.
    ReplyDelete
  5. is not working then.
    I emailed godaddy support but they were not helpful and said that it is not supported.
    what could be wrong?
    ReplyDelete
  6. oh now I see what you are saying, it is in the root dir of the res subdomain, I was trying to do it in the root of all the site.
    ReplyDelete
  7. Awesome, the rewrite works splendidly - I added the same thing for css files and I'm golden.
    ReplyDelete
  8. I had success creating gzip files on the godaddy server using your script for ssh and .htaccess

    However when I test for gzip it says compression is not being used. Is there another script that is missing to tell the server to use the gzip files?
    ReplyDelete
  9. You need to have the .htaccess file. If none was there before, the first time it can take more than one hour for godaddy to "view it".
    ReplyDelete
  10. Why did you exclude Safari? Any reason for that?
    ReplyDelete
  11. The code was adapted from the Drupal post this article links to. They excluded Firefox... I am wondering why: perhaps Safari does not handle correctly this rewriting...
    ReplyDelete
  12. huh. Seems to be working for me fine in Safari. I just removed that line.
    ReplyDelete
  13. what is the syntax for adding more file types? I want to compress my .css .js. and .swf files
    ReplyDelete
  14. Sorry to double post, but I think I figured out the answer to my own question. I added "|*.swf.gz" in the files tag attributes. I didn't bother with running the server side gzip compression, so I compressed the files manually using 7zip and uploaded them to my resources directory (remembering to delete the originals). Problem is, I'm not sure they are being reported correctly. GZIP TEST tells me that they are zipped, and the page displays them so obviously they are being parsed correctly, but google page speed still tells me they need to be compressed?
    ...
    I just had a thought that it might be a local caching issue, but no, that's out. Any ideas?
    ReplyDelete
  15. Hi, what if I want this for my drupal site domain?
    And more files?

    Thanks.

    Sorry, newbie here! :)
    ReplyDelete
  16. Nice one, there is actually some good points on this blog some of my readers may find this useful, I must send a link, many thanks.
    ReplyDelete
  17. Nice blog. I will keep visiting this blog very often.
    ReplyDelete
  18. Amazing blog, I am regular visitor of this website, keep up the good work.
    ReplyDelete
  19. This is what I am looking for. Recently I have problem with my godaddy hosting relating to slow webpage. I am going to put the code on my .htaccess hopefully it will solve my problem.
    ReplyDelete