Get 25% off any hosting plan at Hostgator. Use coupon code wphosting9 at Hostgator checkout

How to Compress and Cache with Godaddy hosting

Want to speed up your Website load times (including Yahoo YSlow and Google PageSpeed performance)? There are various options of which many include adding a bit of text to the .htaccess file. For those using Godaddy shared hosting a workaround (hack) is needed as Godaddy doesn’t like the usual .htaccess means of compression and caching. The method used in this tutorial uses PHP to compress CSS and Javascript as well as .htaccess to cache images.

Note: Please read Jared’s Comments Below

This solution to speeding up your Godaddy hosted Website has 4 actions:

  1. Create a PHP file to compress CSS
  2. Create a PHP file to compress JavaScript
  3. Save the two new PHP files to the root of your Website
  4. Add entries to your Website .htaccess file to run the two compression scripts and cache images (including ico images).

Here are more detailed instructions:

1. Create a PHP file to compress CSS

Create a file called compress-css.php (this is likely best created in a text editor such as Notepad or Notepad++) and save it on your local computer. Copy and paste the following into compress-css.php:

<?
ob_start("ob_gzhandler");
header("content-type: text/css; charset: UTF-8");
header ("expires: " . gmdate ("D, d M Y H:i:s", time() + 302400) . " GMT");
header("Cache-Control: max-age=302400, public, must-revalidate", true);
echo "/*\n";
echo "Generated by www.wordpresspartner.com\n";
echo "*/\n";
echo file_get_contents($_GET['file']);
?>

2. Create a PHP file to compress JavaScript

Create a file called compress-js.php (this is likely best created in a text editor such as Notepad or Notepad++) and save it on your local computer. Copy and paste the following into compress-js.php:

<?

ob_start(“ob_gzhandler”);

header(“content-type: text/javascript; charset: UTF-8″);

header (“expires: ” . gmdate (“D, d M Y H:i:s”, time() + 302400) . ” GMT”);

header(“Cache-Control: max-age=302400, private, must-revalidate”, true);

echo “/*\n”;

echo “Generated by www.wordpresspartner.com\n”;

echo “*/\n”;

echo file_get_contents($_GET['file']);

?>

3. Save the two new PHP files to the root of your Website

Copy compress-css.php and compress-js.php to the root of your Website (this is usually achieved via FTP).

4. Add entries to your Website .htaccess file to run the two compression scripts and cache images

Add the following to the end of the .htaccess file (usually at the root of your Website). If a file named .htaccess doesn’t exist then create a file named .htaccess in a text editor, add the text below and then transfer to the root of your Site.

# BEGIN Compression and Caching Script per http://wordpresspartner.com

# This calls the ‘compress-css.php’ and ‘compress-js.php’ files

<ifModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^(.*\.(css))$ compress-css.php?file=$1

RewriteRule ^(.*\.(js))$ compress-js.php?file=$1

</ifModule>

# This enables caching

<ifModule mod_headers.c>

<filesMatch “\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$”>

Header set Cache-Control “max-age=2592000, public”

</filesMatch>

<filesMatch “\\.(xml|txt)$”>

Header set Cache-Control “max-age=216000, public, must-revalidate”

</filesMatch>

<filesMatch “\\.(html|htm|php)$”>

Header set Cache-Control “max-age=1, private, must-revalidate”

</filesMatch>

</ifModule>

# End Compression Script

P.S. The method as described above has been tested with numerous WordPress Sites hosted by Godaddy (e.g. this WordPress Site uses this exact caching and compression method) however please leave a comment of you encounter errors or need more information.

Related posts:

  1. High Performance Google Analytics for WordPress

31 Responses to “How to Compress and Cache with Godaddy hosting”

  • [...] I found a better solution here, which involves some htaccess trickery to compress all your CSS and JavaScript files automatically. [...]

  • Lynn:

    I’ve followed your directions but I get a 500 internal server error. Any suggestions?

  • admin:

    Hi Lynn,
    Unfortunately error 500 isn’t specific enough to provide any indication of where the error lies.
    Can you provide further info?
    Thanks

  • How do i manage having the php files in a sub folder? the way my site is setup, I have a .htaccess file in the root that renames all paths to go to index.cfm, such as http://www.tentonaxe.com/blog/04/2010/somepost.html becomes http://www.tentonaxe.com/index.cfm/blog/04/2010/somepost.html.

    consequently, all of my assets(images, js, css, etc) reside inside of an assets folder. can this script be modified to work from that assets folder rather than having to include this code in my global .htaccess?

  • In the usual fashion, I figure out a solution myself shortly after asking. I’m sure there’s a better way to do it, however, I just appended /assets/ before the compress-css.php and compress-js.php. too simple. Thanks a lot for this post, I’m recommending it to a friend of mine.

  • admin:

    Great that you’ve managed to get the solution. I like your Site Kevin :)

  • Hello, I’ve been trying to figure out how to add gzip to my site for a while now. I’m glad to learn it’s a godaddy issue that I can solve. I’m having trouble, though. I created and uploaded the compress css and js files. Then I added the code given above to the end of my .htaccess. Then I got the following error:

    500 Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, support@supportwebsite.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.
    Apache Server at cookieandkate.com Port 80

    I switched everything back to the way it was for now. I already had a .htaccess file with some coding in it. I suspect that’s the problem but I have no idea how to fix it. Here’s the .htaccess I tried to upload:

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    # BEGIN Compression and Caching Script per http://wordpresspartner.com

    # This calls the ‘compress-css.php’ and ‘compress-js.php’ files

    RewriteEngine on

    RewriteRule ^(.*\.(css))$ compress-css.php?file=$1

    RewriteRule ^(.*\.(js))$ compress-js.php?file=$1

    # This enables caching

    Header set Cache-Control “max-age=2592000, public”

    Header set Cache-Control “max-age=216000, public, must-revalidate”

    Header set Cache-Control “max-age=1, private, must-revalidate”

    # End Compression Script

    I don’t know what the original code does, nor do I know how to resolve the conflicts in the code. Your help would be greatly appreciated. I’ve been pulling my hair out over this gzip problem!

    Thank you,
    Kate

  • admin:

    Hi Kate,

    My advise would be to wait until Godaddy have the mod_pagespeed Apache module up and running (http://eckstein.id.au/8196/search-engine-optimization/godaddy-wordpress-mod_pagespeed/). That should help load speeds significantly.

  • Thank you, I’ll wait for things to speed up on their own. Thanks!

  • I doesn't matter:

    Just put:

    at the first line of you php template o the php page you want to gzip

    and:

    at the end, and that’s it. Your site hosted on godaddy will be gzipped.

  • Vlas:

    BTW, to check if it works or not you can use http://siteloadtest.com

  • buvanesh:

    Good Day. I’ve followed the directions but for CSS and JS, the Cache-Control is having multiple values. For Ex, the following is the Cache-Control value for a css file:
    Cache-Control max-age=1296271597, public, must-revalidate, max-age=1, private, must-revalidate
    I think the values in htaccess is being appended. How do i solve this?

  • To anyone receiving a 500 internal server error:

    When you copy and paste the code from this page, the quotes are replaced with ‘curved’ quotes because of the font used on this website.
    Simply replace all quotes with plain text quotes ( ” ) and everything should work fine!

    Great script, awesome for GoDaddy users … cut my site load time by 50%.

    Cheers!

  • I still cant see any expires headers in YSlow?,
    My site typically loads in 4 to 5 seconds but has an issue of locking up when we get over a hundred people on the site at once.

  • admin:

    Hi Seahawkfanatic. What are people doing when the SIte ‘locks up’ i.e. are people watching video on your Site?

  • Matt:

    Any idea how to apply this for Drupal?

  • admin:

    Hi Matt,
    No idea how to do this for Drupal. Sorry!

  • Ryan:

    Hello,

    Is there a way to do this with if our middle-ware is Coldfusion?

    Thanks.

  • Ben:

    When using this method I am losing all the css formatting on my site…any ideas why this would be happening?

  • admin:

    Hi Ryan,

    No idea …. sorry.

  • admin:

    Hi Ben,
    Unfortunately various Themes and Plugins don’t like some methods of compression :(

  • Crystal:

    Any idea how to modify this for a non wordpress site?
    I am using PHP 5 and a database driven ecommerce site and would like to be able to set expires or cache at least for the images to improve results on Google Pagespeed.
    Unfortunately with Godaddy Linux shared hosting, I always seem to get a 403 error whenever I try to do much of anything with a .htaccess file.
    Of course Godaddy is close to useless for help with such issues.

    The easy fix may be the mod_pagespeed. They now have the mod_pagespeed on Godaddy but only with the 4GH hosting. I’m checking to see the pros and cons of 4GH.

  • Susanne:

    Worked like a charm for the first of my sites on Godaddy. Very pleased. However, once I placed the same scripts/file into the root folder of another of my sites my index page for that site quit fetching .js and.css rendering the site as text only. Once I removed the scrips/file the site went back to normal. I also tried placing the scripts/file into the html root at Godaddy where all my sites are located. But that did have no effect on any of the sites. What am I missing here?

  • Jared:

    This solution creates a massive hole in your website.

    First let me say that I like the solution. It’s very easy to implement and transparent to the end user. Also, once it is implemented, you can add new CSS and JS files without worrying about the compression.

    The worrisome part though. If someone knows that you are implementing this solution all they have to do is go to your url like so http://www.domainname.com/compress-css.php?file=wp-config.php and they can see all your database connection info. Replace “wp-config.php” with whatever important file you want and the solution will read the actual file instead of rendering it. Or read files that aren’t supposed to be downloaded like the .htaccess file itself.

    The way to fix this is to change the files to check first if the file exists. You do this to make sure that a unmanaged error isn’t returned. Then you check to make sure the requested file is a valid css or js file by verifying the extension. If any of these are false, return a 404 error.

    Example:

  • Jared:

    Sorry. My code wasn’t escaped sit was omitted. Here is the example:

    $file = $_GET['file'];

    if (file_exists($file) && strtoupper(pathinfo($file,PATHINFO_EXTENSION))==”CSS”){
    ob_start(“ob_gzhandler”);
    header(“content-type: text/css; charset: UTF-8″);
    header (“expires: ” . gmdate (“D, d M Y H:i:s”, time() + 604800) . ” GMT”);
    header(“Cache-Control: max-age=604800, public, must-revalidate”, true);
    header(“Last-Modified: “. gmdate(“D, d M Y H:i:s”, filemtime($file)).” GMT”);

    echo file_get_contents($file);
    }
    else
    header(‘HTTP/1.1 404 Not Found’);

  • admin:

    Hi Jared,

    You are absolutely correct. Thank you for noting this!

  • Jared:

    No problem. Just making sure everyone is safe.

    I love the solution btw, I’m using it not only in my WordPress sites, but in all my GoDaddy sites that don’t have mod_gzip.
    It’s the cleanest and most transparent solution out there.

  • moses:

    Thanks admin and Jared for this – its fantastic and my sites load much faster now

  • So, Admin

    Please correct your script on top of the post as suggested by Jared.

    It has bug

  • Jon:

    I also had the same issues with theme disappearing. I even tried it with the Twenty Eleven 1.2 theme from WordPress team and that didnt work either.

    I replaces all the curly quotes with straight quotes and did the same for single quotes.

    I have stared at it and it all looks so simple; I dont see what could be wrong. Help would be great.

    # END WordPress
    # BEGIN Compression and Caching Script per http://wordpresspartner.com
    # This calls the ‘compress-css.php’ and ‘compress-js.php’ files

    RewriteEngine on
    RewriteRule ^(.*\.(css))$ compress-css.php?file=$1
    RewriteRule ^(.*\.(js))$ compress-js.php?file=$1

    # This enables caching

    Header set Cache-Control “max-age=2592000, public”

    Header set Cache-Control “max-age=216000, public, must-revalidate”

    Header set Cache-Control “max-age=1, private, must-revalidate”

    # End Compression Script

  • This works great for my site offline via my localhost with XAMPP.

    However, when I try to implement the same thing online, it breaks the site. I’ll run the Google Extension Page Speed and it tells me that the links to all my css files are bad. However, the addresses are correct.

    Some specs:
    Using GoDaddy Shared Hosting Account
    The website is WordPress based, but I’m not using pre-baked template.
    The entire site is currently in a folder domain.com/demo/ so I put the .htaccess file in the demo/ folder. However, thinking this may be a problem, I moved the site to the root. Same issue. Broken site as if the css or js files don’t exist.

    Any thoughts? Why would it work so well offline and not online? This makes no sense to me

Leave a Reply