Wordpress Cache Plugins Not Always The Best Solution !
Wordpress cache plugins are not the right solution for everyone, so could Apache htaccess be a better solution for site speed than Super Cache or Total Cache.?
You may recall a recent article where I compared two very good Wordpress Cache Plugins WP Super Cache & W3 Total Cache, the later in my opinion being the better of the two.
However it now appears that there are many blooggers out there that for whatever reasons, are having immense problems with either of these cache plugins. I know first hand how frustrating & annoying this can be, so I now offer you a 3rd alternative and the one I use myself on this blog, Apache htaccess! This little file is the one that deals with all your permalinks & any other server side instructions on a Linux server, so if your blog is running on a Windows server please DO NOT ATTEMPT THIS.
Step 1. Backup Your Database
I cannot over stress the importance of backing up your database before proceeding any further. Whilst I will give you the exact entries to use in your htaccess file, our setups are not identical & therefore the possibility of an error is always present. By backing up your database you always have the option to turn the clock back a restore your previous settings.
Step 2. File Compression
There are two main elements to speeding up your site load times, these are caching of files & of course file compression. If your on shared hosting & your blog host allows compression to be enabled (and most do not), then make sure you enable compression before setting up caching, as there is little point in caching uncompressed files. If however your host does not allow compression to be enabled, then continue on and we will deal with this later in the article.
Step 3. Find Your htaccess File
Using your FTP client open the folder “public_html” if Wordpress is installed in your root directory, then you will see such folders as wp-admin, wp-content & wp-includes, if Wordpress is not in your root directory then open the relevant folder to your Wordpress blog !

The image above shows the htaccess file we wish to deal with & it should always have these three wordpress folders wp-admin, wp-content & wp-includes above it, that way you are assured of having the correct file as it is possible to have more than 1 htaccess file in your setup.
Now do not double click this htaccess file in an attempt to open it, instead right click the file & then select edit. Providing you have disabled and removed any cache plugins, then basically what you should see is in the image below;

This is a typical basic htaccess file with standard Wordpress entries
Your own htaccess file could possibly have other entries dependant on what plugins you are using, this should not be a problem as long as there are no entries from any type of cache or compression plugins. If you do notice any entries from a cache or compression plugin, then please disable that plugin before proceeding further.
Entries to add to your htaccess file
Copy and paste the entire entry below into your htaccess file, leaving an empty line between what you paste in and what is already there. Actually the ideal place is just after the End Wordpress entry.
FileETag None
# HEADERS and CACHING
##############################################
#### CACHING ####
# 3 MONTH
<FilesMatch “\.(flv|gif|jpg|jpeg|png|ico|swf)$”>
Header set Cache-Control “max-age=7776000″
</FilesMatch>
# MONTH
<FilesMatch “\.(js|css|pdf|txt)$”>
Header set Cache-Control “max-age=2592000″
</FilesMatch>
# WEEK
<FilesMatch “\.(html|htm)$”>
Header set Cache-Control “max-age=604800″
</FilesMatch>
# DONT CACHE
<FilesMatch “\.(pl|php|cgi|spl|scgi|fcgi)$”>
Header unset Cache-Control
</FilesMatch>
Note: The values used above are all in seconds and can be altered as follows;
1 Day …..= 86400
1 Week.. = 604800
1 Month = 2592000
3 Month = 7776000
6 Month = 15552000
1 Year… = 31536000
WARNING
I would advise against using the really high values until you are completely happy with the setup, as any errors would mean having to rename a large amount of files & images should you make a mistake. So personally I would set all values to 3600 initially as this is only one hour to wait before the cache clears & takes new copies. Once you are happy that all is functioning correctly then I would set the values to those initially indicated in the code you copied across to your htaccess file, but only after reading the next section on compression.
File Compression Using Gzip
Now if you are on shared hosting, it is very likely that your host does not allow compression to be enabled and although some will clearly state that compression is enabled, you will often find that output compression is not. What this basically means is that your server is setup in such a way that it can receive compressed files but will not send compressed files. An easy way to test this without having to take your hosts word on the matter, is to create a simple txt file with the following entries;
<?php
phpinfo();
?>
Save this file as phpinfo.php & upload to your root directory & then browse to www.your-domain.com/phpinfo.php
This will put all the php info that runs your site up on the screen for you to read
What we are looking for in this file, is an entry as follows;

This shows that compression is not fully enabled.
Or
This shows compression is fully enabled
Now if it should turn out that you discover your host is not or has not enabled compression, then all is not lost, we can still get compression by adding a line of php code to our wordpress files as follows;
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)) ob_start(“ob_gzhandler”); else ob_start(); ?>
This code compresses on the fly (before sending requested files) & needs to added to the top of your file imediately before the line that starts with <?php. Adding this line of code can be a little hit and miss simply because of the number of files involved & the hierachy of these files, for example lets say you add this line of code to your index.php but then your index.php file calls your header.php file also with the code added, this will produce an error as you are now trying to compress the same content twice.
Example:
index.php
|
header.php
|
page.php : single_post.php : archive.php
|
footer.php : sidebar.php
When I used this method I opted for page, single post & archive which gave a pretty good result, but adding the code to any 2 lines above will give you errors. So the best advice I can give is to look at your theme structure, where & how each file is called & then place the code accordingly & then test for compression at any of the following sites;
1. WhatsMyIP
Compression & Caching Sorted..?
Assuming all has gone to plan, you should now have compression & caching fully setup so the final step now is to pay a quick visit to WebPageTest.org and do 3 site speed tests;
Speed Test Results For My Blog Review

As they say “the proof of the pudding” etc. This clearly shows that both compression & caching are now fully functioning and my site load time has been cut from 8.5 seconds (according to Google), to a mere 3.91 seconds for an uncached visitor & 1.604 seconds for a cached visitor, now this I can live with hahaha
So I now have no cache or compression plugins & my site is now loading faster than it ever has, so my friends if the Wordpress cache plugins are not working for you, then try this method instead and please do let us know how you get on..?
|
|
|








Very comprehensive break down of the actions required to impliment the tool, great stuff, thanks for sharing.