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:

  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.

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

Leave a Reply

Contact, Links, Sitemap Sydney WordPress

Loading