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.
This solution to speeding up your Godaddy hosted Website has 4 actions:
- Create a PHP file to compress CSS
- Create a PHP file to compress JavaScript
- Save the two new PHP files to the root of your Website
- 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.
[...] I found a better solution here, which involves some htaccess trickery to compress all your CSS and JavaScript files automatically. [...]
I’ve followed your directions but I get a 500 internal server error. Any suggestions?
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.
Great that you’ve managed to get the solution. I like your Site Kevin