Optimizing WordPress For Shared Hosting
This post is now quite old, please feel free to read it, but I would advise that you read my new version of this post as well. You can find it here.
WordPress is actually fairly lightweight on a default install, but there are quite a few plugins essential for a good SEO friendly WordPress install. With those installed you can easily end up with a footprint of between 20-30MB per request. On a shared server, with say a memory limit of 90MB, you are pretty much up the creek without a paddle it 3/4 people enter your website. Here I am going to share my top tips for making WordPress work it’s best in a limited resource environment.
Minified Scripts
The first tip is a small one. Whenever possible use the minified versions of scripts such as jQuery. This doesn’t really help much with memory, but does give a little bit of a speed increase. Feel free to skip this one, but if you are all about squeezing out those few extra milliseconds then give it a go.
WP Super Cache
My all time favorite caching system for WordPress has to be WP Super Cache. It has always worked wonderfully on my shared hosting environment. It can be a little tricky to set up, but there are great instructions provided and it’s worth it. I, personally, find having ‘Cache Rebuild’ and ‘Coarse File Locking’ switched on helps too. I also have compression switched on, although that tends to only help with bandwidth usage.
Reduce Amount Of Plugins
This may seem obvious, but the less plugins you have the less memory you are likely to use. You might also want to look out for plugins that load scripts/options all the time, even when they are not needed. This can add extra weight to WordPress by loading information even when the plugin is not required to do anything.
Timthumb!
Otherwise known as a dynamic thumbnail generation script, Timthumb uses PHP & GD to create alternate sized thumbnails. It is quite often in themes, especially magazine style themes to create thumbnail sizes WordPress doesn’t normally create. While Timthumb does actually have a cache to store the images it creates it isn’t as efficient as a WP Super Cache style cache.
More often than not you do not want to spawn a PHP process when it isn’t needed, especially on shared hosting. Why? Well because loading a WordPress page takes, as I said before, about 20-30MB of memory. If you can load a page without PHP spawning you won’t use large amounts of your available memory & you could probably survive a front page feature on Digg. That is what WP Super Cache does, it saves the result of PHP processing your request for a page to a flat HTML file & redirects you to that flat file using Mod Rewrite (Apache Module) the next time you want to view it.
What’s That Got To Do With Timthumb?
Well Timthumb caches images after they are created, which is great. The problem is that to find if an image is cached it needs to use PHP, but you don’t want to run PHP as a normal PHP process still uses between 8-13MB of memory. That’s memory you can’t afford if WP Super Cache is re-caching your page (20-30MB + 8-13MB = 28-43MB). Therefore the best option is to modify Timthumb to work exactly as Super Cache does, using Mod Rewrite to direct you to the cached file. This will probably make this post huge, but here’s how I got mine working. A big thanks to 2 examples (linked to below) of Timthumb being used with Mod Rewrite I found on the web, although the exact same code didn’t work for me, the code here is only very slightly modified.
First you need to find this section of code in Timthumb:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function get_cache_file ($mime_type) { global $lastModified; static $cache_file; $file_type = '.png'; if (stristr ($mime_type, 'jpeg')) { $file_type = '.jpg'; } if (!$cache_file) { $cache_file = DIRECTORY_CACHE . '/' . md5 ($_SERVER ['QUERY_STRING'] . VERSION . $lastModified) . $file_type; } return $cache_file; } |
Now comment out the highlighted lines, and/or replace them with these lines:
1 2 3 4 5 6 7 8 |
$filename = clean_source(get_request( 'src', 'timthumb' )); $filename = pathinfo($filename); $cachename = $filename['filename'] . '-' . get_request( 'w', 100 ) . '-' . get_request( 'h', 100 ) . '-' . get_request( 'zc', 1 ) . '-' . get_request( 'q', 80 ); $cache_file = $cachename . '.png'; |
Thanks to speedingupwebsite.com for some of this code. We basically change Timthumb so it doesn’t md5 the filename, as we need a nice sensible filename for us to rewrite to.
Next we can go onto the Mod Rewrite. I can’t deny that Mod Rewrite is powerful, but I hate the stuff so please forgive any mistakes I make here.
1 2 3 4 5 6 7 8 |
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{SCRIPT_FILENAME} timthumb\.php RewriteCond %{QUERY_STRING} src=http:\/\/.*\/(.*)\.(png|jpe?g)&w=([0-9]+)&h=([0-9]+)&zc=([0-9]+) RewriteCond %{DOCUMENT_ROOT}/wp-content/themes/returntrue/thumbs/cache/%1-%3-%4-%5-80.png -f RewriteRule ^.* /wp-content/themes/returntrue/thumbs/cache/%1-%3-%4-%5-80.png [L] </IfModule> |
Again thanks to speedingupwebsite.com & also thanks to hungryhacker for the code here. I’ve modified it (very slightly) as while it was very helpful it just refused to work for some reason.
Let’s explain what’s going on here. First we check if the script we are asking for is timthumb.php
. If you called the script something else change that part. The next part checks the query string. We want to match the images filename, the width, the height & if zoom crop is turned on. The sections in parenthesis can then be used in the next part to fill in the new filename we gave thumbnails saved by timthumb. We use %{DOCUMENT_ROOT}
to get the path to the root of our site & then we fill in the path to the cache folder created by timthumb. The -f
tells Mod Rewrite to only return true if the file actually exists. If those three conditions are all true we try to redirect the request for timthumb to the actual saved image file. If those rules are not true it will hand the query to timthumb as normal.
This has worked wonders keeping the strain on the server down for my website. Just remember that you will need to change both the paths to where your timthumb cache is. It works in exactly the same way as Super Cache, redirecting requests to Timthumb to the actual image file should it exist.
There we have it. My list of ways to help optimize WordPress for running on shared servers. I just want to make it clear that the Timthumb Mod Rewrite cache was not my idea, the code is a slightly modified version made with help from the two sites I linked to above. All credit goes to them for the idea & the code.
I hope this post was useful & it has helped you optimize your WordPress install. I currently have all of the tips here implemented here on Return True & I’ve noticed a dramatic increase in performance. As always if you have any questions about anything in this post, please post them in the comments.
111 Comments
fwaggle
Hi there,
I think the reason my mod_rewrite rules “don’t work” is because mine involve modifying the WordPress theme so it never actually looks for timthumb if the cache file exists.
The other site you linked to takes the different approach of poaching the query string from the URL to timthumb, and using that to look for the cache file. That has the advantage of working with most any theme that uses timthumb, without having to modify the theme.
I wish I’d googled before I went through all that effort, because the other site you linked to already had all the information right there – I really thought I was onto something. 🙁
Paul Robinson
Actually your rules helped a lot. I’m still learning to use mod rewrite so they helped me understand it a bit more. I did try your method of changing the file paths, but when I found the other version I decided to go for that due to the fact I needed to implement it over 3 or 4 sites all with different themes.
I went looking too as I needed to stop PHP running at all to help with server stress. I wasn’t going to post it as it’s technically just repeating what’s already out there, but I though maybe some people may not be able to fix the problem I had with the mod rewrite rules. I’m not sure why they didn’t work for me, but it was only a slight modification to the filename matching in the rule. It matched the entire file path except for
http://
and I only wanted the filename.Your post still helped a lot so thanks for writing it. 😉 Also I hope you don’t mind that I wrote this out. I just wanted to share how I got it working just in case it helps anyone.
fwaggle
“I’m still learning to use mod rewrite so they helped me understand it a bit more.”
Good luck with that – mod_rewrite is voodoo[1]! That’s basically why I edited timthumb the way I did, it’d involve minimal understanding of mod_rewrite. The speedupwebsite method seems a lot cooler, I just can’t be bothered messing with it. 🙁
Anyway, just thought I’d stop by and say thanks for the pingback. 😀
1) http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
Paul Robinson
😆 To be honest I hate Rewrite. It is the bane of my existance, but it’s cool & can work wonders so I kinda put up with it.
The thing I hate about Mod Rewrite is the fact that if you make a mistake (and you can’t enable logging) you’re basically left guessing what is wrong, if the problem isn’t obvious. 🙁
Yeah, I had no choice but to use the other method. While I could have changed the paths to Timthumb it was quicker & easier (for clients) to just upload the new files with modified paths.
No problem. 😉
LivingOnDividends
This is great. You should submit this to the TimThumb author to integrate.
Have you seen Smart Image Resizer? http://shiftingpixel.com/2008/03/03/smart-image-resizer/
I’m deciding between TimThumb and it right now. Supposedly it’s even better. It does cache too. And it even sharpens images. Love to know your thoughts on it vs TimThumb.
Paul Robinson
Wow… Really sorry I didn’t reply.
I would advise against all image dynamic image resizing scripts in WP now that it has it’s how custom thumbnail creator.
It takes a little while (if you aren’t a dev) to figure out how to integrate it into your theme, but it works a lot better as the images are created on upload. Along with Viper007Bond’s plugin for regenerating your thumbnails (so you can use it retroactively) it is great to get rid of server troubles caused by timthumb. 🙂
Tinh
Great tips, I was referred by HostGator to this post, really appreciate this
Paul Robinson
Wow! Really? 😆
Thanks, glad you found the post helpful.
Budi Hadmajaya
good article, I also recommended by HostGator
Chuck
I’m curious why you’re still concerned about timthumb, regarding the new built-in thumb method of WP.
As I totally appreciate the work that has been published here, I’m excited to get your statement considering the omnipresent timThumb vs wp thumb generator, and of course taking TAI into discussion as well!
I was searching a while, but a clear statement has never been posted anywhere… few just state timThumb is bugged, wp’s inner method is too shallow etc. -> However, I’m actually at the point to create a category-related post-thumb grid, which forces me to grapple with the whole thumb thing, hence it would be dumb to utilize timthumb if it’s out of date!
Chuck
I found a subtle statement in your recent “Using The Attached Image With Custom Post Thumbnails In WordPress” tutorial…however, this leads me to add: ” What’s if there already exist around 100 pictures?” … isn’t it smarter to let the timthumb/tai combo produce thumbs via url instead of the wp method?
Paul Robinson
You may notice that this post was from a few month ago. At the time I hadn’t had the time and hadn’t need to delve into the usefulness of WP’s new post thumbnails feature.
However I have since then & the answer is, it’s entirely up to you. WordPress’ new post thumbnail system is great, it allows you to create custom thumbnail sizes without on-the-fly generation script like timthumb. You are right in that if you already have pictures it’s a pain, but that’s easily sorted with Viper007Bond’s regenerate thumbnails plugin.
The biggest problem comes in that you will still have to go through all your posts to set a featured image or WP will not show one. However if you check out my latest post you can see I’ve updated The Attached Image to help out with that.
Also sorry for the delay & the down time. Someone decided it would be funny to attack the site. Luckily Mod_Sec caught it, but it still took the site down for a little while.
Daniel Noll | Uncornered Market
I plan to use TimThumb to display resized thumbnails of Flickr images associated with posts.
I came to your article because I was looking at a way to clean up the resulting image source URL (it’s a bunch of timthumb directories and code, followed by the URL of the Flickr image). In other words, a mess.
Is it possible to use your above approach to redirect the image source URL to something a bit more SEO-friendly, while achieving website speed optimization at the same time?
Or should I be thinking of this another way?
Paul Robinson
You probably could yes, although it would probably require you to edit the timthumb file as to point to the cleaner nicer URL & have mod_rewrite catch the clean URL & transparently redirect to the ugly URL.
I’m no expert with mod_rewrite though so the code to do it would probably be a case of trial & error while looking at the rewrite log (if you have access to them).
Robson
Thanks !!! I was referred by HostGator (via E-mail) to this post, really appreciate this !!
Paul Robinson
No worries. I guess HostGator like my post your one of a few people have referred here. 😆
Glad it was able to help you out. 😉
Beth Edwards
Hi I was sent here by HostGator also lol However I have a problem, I am not that savvy at all this and have no idea where to get to that Timthumb file 😐 Please go gentle on me 🙂
Paul Robinson
Hi Beth,
I guess the question is do you need it? Timthumb is just mentioned here as it is one of the most common causes of rapid memory use in a WordPress environment.
If it isn’t provided with your theme then you don’t need it.
If you aren’t sure if it is provided with your theme you will need to look in the folder of your theme & look for a file called
thumb.php
ortimthumb.php
. It’s sometimes in a folder also named thumb or timthumb.Your theme is generally found at
http://sitename.com/wp-content/themes/
once there look for a folder with the same or similar name as the theme you are using in WordPress.Quite a few modern themes are now using a different, more efficient method to generate their thumbnails so they probably won’t have timthumb.
Also if you haven’t followed the tip about WP Super Cache yet, I’d advise checking out W3 Total Cache instead. I’m using it on this site & it’s reduced the load on my shared server an amazing amount.
Sorry, if I’ve rambled on a little there. Hope that helps you out. Let me know if you need any more help. 🙂
Paul Robinson
Also just noticed your site is showing a 403 forbidden error at the time of writing this comment. Just in case you hadn’t realized.
Beth Edwards
The 404 is the reason I am here 🙁 They have suspended me until I can get this sorted out. Seems my site is too popular which is great except now I am calling on the database too much. Theysent me to read this and grab the W3 Total Cache which I have just uploaded and am waiting for them to let me in again to activate.
I also added it to my interview section there which wasnt turned off.
Thank you so much for your fast response.. off to look in my theme files now.
Paul Robinson
No problems, glad to help.
That happened to me as this site got more popular. Although I must admit my host was a little more patient & didn’t disable my site. 😆
Yeah W3 Total Cache is a much better choice if your site is extremely popular as it bundles a lot of features in the WP Super Cache doesn’t have.
Let me know if you need any further help & I’ll be happy to help as much as I can.
Beth Edwards
ALL tips greatfully received. I had just got some famous names up and this is killing the buzz 🙁 I cannot have this happen again but, until I can afford sharing with just one other or my own server I am stuck 🙁 I believe they are letting me in now so I am off to set my plugin up now *sigh* again, thank you very much, I really appreciate it 🙂
Paul Robinson
No worries.
Good luck, and feel free to ask if you need help with anything else. You can always get me via email if it’s an emergency. 😉
Beth Edwards
I am having hell and all problems lol Can you think of any reason why I now need a trailing slash on the end of all my blogs or I get a 404? Its only since I put the WP3TC on 🙁 and I dont understand one word of the settings ROFL
Paul Robinson
Hi Beth,
W3TC can be confusing to set up, but if you can do it it’s worth the effort. I’m not sure how to set it up for hostgator since I don’t have experience with their hosting.
On my hosting though it’s just default settings all the way, unless your a control freak like me.
I do know that if hostgator are anything like godaddy they are a tad OCD with locking their files (Chmod) & W3TC may not have been able to write it’s changes to your .htaccess file. Hence the problem with trailing slashes.
If you SFTP/FTP into your site and open your .htaccess file (you’ll need to show hidden files if using FTP) and at the same time go into your WP admin and open the install option of W3TC from the menu. If you scroll down a little you should see two large pieces of code to place in your .htaccess file. Put it at the top of it and reupload it to the server.
That will hopefully fix your problems. Fingers crossed & please let me know how you get on. 😉
Beth Edwards
🙁 already done that. Im so pooped now 🙁
Paul Robinson
Hmm. My last thought is maybe try disabling W3TC and make sure it definitely is W3TC that’s causing that problem. Just because it’s the last thing you enabled doesn’t always mean it’s the cause, although it is the most likely.
It still sounds like a problem with mod rewrite to me though. Have you tried refreshing the WP rewrite rules by going to the WP permalink options and resaving the page?
Other than those my best guess after that would be to contact hostgator and send them the mod rewrite rules (contents of htaccess file) and ask them if any of the rules are not allowed as that can cause some strange problems sometimes.
I hope that helps. I’ll keep searching for a solution though just in case it doesn’t.
Paul Robinson
Just found out that apparently having a trailing slash in your permalink structure settings can cause that 404 problem. You aren’t by any chance using a custom permalink structure with a trailing slash on it are you?
Beth Edwards
Um, well not as far as I know but am off now to recheck. it only started after this WP3TC thingy though. I am going to do what you said, deactivate and see what that does, at least then I will know…. I will also check my other settings.
Paul Robinson
It probably is the cause but it’s always a good idea to check.
I’ll keep looking for anything else that could cause the problem. One more question though, does it only have the problem in IE or is it the same in other browsers?
Paul Robinson
The only other thing I’ve been able to find was a bug in an older version of W3TC, but it was fixed in version 0.8.5.2 I think. This is probably a daft question but are you using an old version of W3TC?
If not I’m afraid I haven’t been able to find anything else.
That’s doesn’t mean I’m gonna stop looking though. I’ll find something to help you fix it, lol. 😉
Beth Edwards
I only use Firefox so not sure about IE even .. Have just rechecked my htccess, that’s ok. Now off to deactivate and check that
Beth Edwards
Ok seriously pooped LOL I deactivated the WP3TC so took off the htaccess stuff as well and it works fine. It automatically adds a slash to the end of the blogs when I visit them which it must have done before and I didnt know…. so that means I have it set up to do that somewhere. If I therefore remove that, it SHOULD work with WP3TC but….er… I have no idea where I would have set that up LOL!
Beth Edwards
Version 0.9.1.3
I’m damned if I do and damned if I don’t use it. If I do I get this error and if I don’t I get shut down by my host 🙁
Paul Robinson
Damn. I can’t think of anything else right now off the top of my head.
The only other way I can help is to try and debug the problem myself, but that would require admin access to your WP install. That’s entirely up to you though.
If you do want me to have a look feel free to drop me an email using the contact form and we can sort it out.
I’ll keep looking for other things though. Right now though it’s bed time for me as it’s 01:45 in the morning. 😆
Paul Robinson
Just a small question though what exactly is the problem your getting? I’ve just tried going to a few of your posts/pages and the main page and removing the trailing slash. I get redirected to the page with the trailing slash added as normal? Strange?
Beth Edwards
Yes, as I said in my other post, I deactivated it and it now sends me to the blogs ok, adding a trailing slash. If I reactivate it, it removes the trailing slash and I cannot access them again without manually putting that on.
I will contact you.
Orisha
I’ve been referred herre by hostgator too, I’ve read this post and the sources, and I have a question, do the timthumb corrections you mentioned do work with W3 total cache too? because I think that’s what overloading the cpu performance in my site and Hostgator is recommending to use W3 instead of super cache
Paul Robinson
Hi Orisha,
I currently use both W3TC and the timthumb .htaccess fix, and they seem to coexist without any problems.
Timthumb uses a lot of CPU because of GD so there is a very good chance they are right. Using the .htaccess fix will only stop the pre-requisite call to PHP before finding the cached image though not the drain on CPU resources timthumb causes.
You could always open up timthumb and make sure the line that says:
isn’t still on 250 like above. For a large site that uses timbthumb for every post you’d probably need this quite high like maybe 25000 or something.
If you don’t every time the cache folder contains 250 images it will delete 5 and remake them even if the images haven’t changed in anyway.
I hope that helps you out a little. Let me know if you need any more help. 😉
Orisha
Thanks Paul, I’ll do what you recommend and tell how it went.
Thanks a lot for the quick response
Paul Robinson
No worries. Hopefully that will work for you.
Orisha
Hi Paul
It turns out that the index.php file is the one who’s causing the problem, but now I’m stuck at this point and don’t know what to do to fix it. I’m pretty lost with this, can you help me with this please?
Paul Robinson
Do you mean index.php is the file that is spiking the CPU usage? If so that could be anything as everything in WordPress is routed through that file. It’s most likely PHP in general creating the page that is causing the problem.
You’d normally use a caching plugin like W3TC to solve that, but if you can’t even generate one page then even caching won’t help you.
The normal WP install would probably take 25-30MB of memory. Try disabling a few non-essential plugins. What is your allocated memory limit?
Orisha
Yes, at least that’s what hostgator says it’s causing the overload, I also thought about the plugins, but I’ve been using the same plugins for quite a while and everything went well.
The only plugin I did disable was the one I used for podcasting a while ago since it was making a mess with the database. (now I’m using 8 plugins)
Adevrtising Manager (I think this one may be causing the trouble but I’m not sure)
Akismet
All in one seo pack
Easy up me
google xml site maps
role manager
w3 total cache
wordpress related posts
How do I look for the allocated memory limit?
Paul Robinson
According to HostGator’s support docs it’s 64MB on a shared hosting & cannot be increased unless you have a VPS plan. 64MB is cutting it awfully close for WP. My shared server uses 90MB.
The only advice I can give really is to put your site into maintenance so people can’t access it, disable all your plugins & see if you can monitor your memory usage through SSH using the ‘top -c’ command or see if you get any memory errors. Then enable each plugin one by one until you find the one causing the problems.
From the list you have given though there doesn’t seem to be many plugins on there that would increase your memory usage much.
You said that HostGator said it was overloading the CPU. Is it the CPU or Memory that is being overloaded? Memory you can fix by reducing the memory used by scripts and normally never gets below 15MB on a WP install (unless you are using caching). The CPU is commonly overloaded by image manipulation scripts such as timthumb.
Orisha
According to the report I think it may be the cpu, They give me a list of running queries the have a defunct tag in the end, and also give a list of the files that are being requested and all of them are images, so i guess it has to be the timthumb script
Paul Robinson
If they were all images then timthumb is most likely the problem. The only thing you can do if you want to keep using timthumb is try out the htaccess rules here & increase your cache number in the timthumb file to a very large number, that way it won’t have to recreate the files very often if at all.
Orisha
Actualy I’ve done that, but a bit search on google made me realize that I might be using an old version of the script (not sure about it cause it came along with the theme and the script has nowhere the date or the version), so now I’m testing and making the modifications that are here and see how it goes
Paul Robinson
Have you still been getting the problem even after you changed the cache size? If so the only other idea I can think of is using ImageMagick instead of GD. ImageMagick tends to save on memory though not CPU so it probably won’t help much.
Your best bet is to contact HostGator (if you haven’t already) and ask them to confirm if its memory or CPU. If it’s CPU and it’s the timthumb that’s being killed then there isn’t much you can do. You could try giving it lower sized image, the lower size/quality of the image given to GD the less CPU/memory it takes to manipulate it.
Orisha
Yes it,s cpu overload, not memory, I’ve been googling trying to find more info about the script causing cpu overload but nothing still, I guess I’ll have to give up using the script and find another way to still use the thumbnails in the posts or maybe changing hosting company
Thanks a lot for your replies and your help
Beth Edwards
Orisha, I am getting this but not with the images and they did not give me half the information they have given you. I am thinking of changing hosts too. Seems they may be squeezing more of us onto the servers than they used to and now we are paying for it.
Paul Robinson
I would advise either moving host or upgrading. From what I can see the amount of memory they give isn’t great and their servers look a little underpowered for the amount of people they look to be putting on each server.
If your site isn’t huge yet try and you still want shared hosting try my host Dreamhost. If you are looking for shared grid hosting and are prepared to pay I’ve heard good things about MediaTemple but it can be expensive even for their cheapest grid service.
I hope that helps you both out a bit.
Orisha
Dreamhost is actually my first option to change hosts since my site is not that big, it handles about 50k visits per month so i guess even the cheapest vps will work and a lot of friends are actually recommending dreamhost
Paul Robinson
I get around the same and Dreamhost handles the site great. As long as you cache it with something like W3TC. My uptime is around 99.97% which I think is great for a shared server. Also their support team is really, really helpful and they don’t give you canned responses out of some book.
Picool
Hi ! Can you make an update of your article with the new version of timthumb because I can’t find the line to edit, it don’t work anymore 🙁
Thanks !
Paul Robinson
Hi Picool,
I don’t use Timthumb anymore, but looking at the new version it’s still fairly similar. The way the file is renamed has changed to one line of code instead of the two I show. I’ll update the code for you now. 🙂
Arran
Hey, I also have the issue that Picool did.
Basically all the images are dumped in the same folder as timthumb.php rather than the /cache folder for some reason.
I’m not quite sure why it does this. The .htaccess gives the path to where the cache folder should be but the images still end up dumped outside the cache.
Even stranger, I think the script is working still.
Paul Robinson
Hi Arran,
I’m afraid I’ve never been able to figure out why that happens. I still use the script here but it uses the older version of timthumb and the images are placed in the correct cache folder.
I will be moving away from timthumb soon though as I prefer the new WP custom thumbnails. It saves a lot of memory by creating all the image sizes need at image upload time.
Picool
Hi, thank you for you fast answer ^^. But don’t work 🙁
When I put the new code (in timthumb) I haven’t thumb anymore…And with the .htaccess, nothing better…
Can you help me ?
Thanks
Paul Robinson
Hi,
I can’t see any reason for it not to work, to be honest there isn’t much of a difference. The code changes just change how the file is renamed & since the .htaccess mod requires a different naming convention is doesn’t have any effect.
I’m unable to test it at the minute as I don’t have access to my testing server. One thing you can check though it to make sure the cached images are being saved with the correct names. Timthumb normally saves them as MD5 hashes but we want them names like this
filename-100-100-1-80.png
. Where the ‘100’s are the width, ‘1’ is if zoom crop is enabled, and ’80’ is the quality.Also are you getting any errors in place of the images? Sometimes they are only visible in the source code.
Picool
I understand, but I haven’t a cache folder and in the source code when I click on the image url I saw a bug.
Paul Robinson
Are you working on a local server or a web based server? You may have some permission problems as timthumbs should have created the cache folder for you. The modifications listed don’t alter the scripts native ability to save images or create folders it just alters the name the image is eventually saved as.
You can try creating the folder yourself as that normally bypasses any problems with permissions you may be having.
Also I’m not sure what you mean by ‘saw a bug’? Can you elaborate on that?
Picool
I work on web based server, about the bug :
I get this when I click on the url in the source code :
Fatal error: Call to undefined function cleansource() in /homez.160/penspinnz/www/wp-content/themes/magazine_10/tools/timthumb.php on line 562</
Paul Robinson
Ahhh okay. Well that just looks like the name for
cleanSource()
has changed. Either that or I’ve gotten it wrong in the first place.Looking at the most recent version of the Timthumb file it looks like it is
clean_source()
now. I’ll alter the code in the tutorial again to reflect the change.Picool
Oh yeah it’s that ! It work now ^^
Thank you so much for your help 😀
Picool
Hum sorry ^^, thumb are display, but in the source code the url don’t change :s
Don’t know why ><
Paul Robinson
Well the mistake was my fault, so sorry about that.
I’m not sure what you mean by the URLs don’t change?
Picool
Hum, I can’t show you a piece of code here…
So, in the source code I saw that :
“http://www.penspinning.fr/wp-content/themes/magazine_10/tools/timthumb.php?src=http://www.penspinning.fr/mods/bonkuracs/bonkuracs.jpg&h=90&w=115&zc=1”
But in the cache folder I have the right name
Paul Robinson
You can show code here using [lang] tags so
will allow you to show html code. Just remember to remove the spaces.
The URL’s aren’t supposed to change as the redirection is done using mod_rewrite behind the scenes. To be honest there isn’t much of a way to tell if it is working properly. Generally though if you have the htaccess file up & you are still seeing images it’s a good bet it’s working properly.
Picool
Humm, how can I see it’s efficient ? Because I would like to remove the query string “?” so it don’t work, I don’t understand…
Paul Robinson
Basically it works like this.
Normally Timthumb takes a image resizes it, and then saves that smaller version to use to save memory instead of resizing again.
However Timthumb still has to invoke PHP to check to see if it has already cached the file. This uses between 18-30MB of memory on it’s own.
This edit makes it so the cached file can be found using Apache (more specifically Mod Rewrite) which is always running. This stops Timthumb for invoking PHP unless it needs to create the cache file.
Unless you have Shell access with access to the unix command ‘top’ it’s very difficult to tell if it’s working. As I said before though, from all the local Unix testing I’ve done, if it doesn’t work you end up with no thumbnails at all. So if you have the Mod Rewrite rules on the server & you have images (and the paths are correct) it’s a pretty good bet it’s working.
Unfortunately you can’t remove the ‘?’ from the query string (not without doing it a completely different way). That is all done behind the scenes by mod rewrite.
Picool
Oh ok, I understand now ^^. Last question : how change the folder of the cache ?
Thank you again for your help
Paul Robinson
You can just rename it then change the name in the top of the timthumb file. Also you will need to change the name in the path on the htaccess file.
Picool
The :
define (‘DIRECTORY_CACHE’, ‘./cache’); // cache directory
And :
RewriteRule .* /cache/%1-%3-%4-%5-80.%2 [L]
Just ? Because it don’t work 🙁
Paul Robinson
Yes those are the lines. You also need to change the
line too.
Picool
You will kill me but I modified those lines and it don’t work ><
Paul Robinson
Hmmm. I’m not sure as those are the only line I know of that reference the cache location. Have you checked that timthumb is saving the images in the new cache folder?
Picool
Yes I check, and the cache folder is the same that where timthumb is but I would like in an other folder (cache)
Paul Robinson
I’m not sure what you mean? Do you mean that you don’t want the cache to be in the same folder as timthumb? If so I don’t know if you can do that.
Picool
I mean that the cache folder still where timthumb is (but I would change that).
But nevermind ^^, thank you for your help 😉
Paul Robinson
Okay, no problem.
Sonda
I think that this is useless. Shared hosting must die-die-die! I hate shared hosted services, bad, slow, support sucks. Go for small dedicated server instead and upgrade as your site grows. You’ll remember me someday.
Paul Robinson
Hi Sonda,
Personally I think it shows incredible ignorance to tar all shared hosting with the same brush.
This site is currently on shared hosting & has an uptime of 99.1% over 2/3 year, I’ve never had any problem with support. I always get a timely, helpful response even on topics out of their support range & even have live support so I don’t have to wait in emergencies.
Reliable & trusted dedicated servers are expensive, even for a small one. I would have to pay nearly £300 a year for a good reliable dedicated hosting service, even for the lowest package.
Kathy Keefe
Wow, I guess I’m not alone in being referred here by Host Gator! Thanks for the great tips. Up and running again in a flash!
Paul Robinson
Hi Kathy,
Nope there have been a few other people referred to this page by Host Gator. 😉
No worries, glad they helped you out. 🙂
facebookskin
thx a lot for your info,
berita hari ini
nice info. thanks for share about Shared hosting
Mukesh
Noobs like me have
a hard time doing the above things.
Paul Robinson
Hi Mukesh.
I can understand having trouble implementing the Timthumb alternative. Now though I’d recommend using the new WP thumbnail system instead. It’s much better & doesn’t use resources as heavily.
Yoav Aner
Thanks for a great post. It inspired me to come up with a similar solution that only requires (some simpler) .htaccess rules for use with cloudfront CDN. No modification of timthumb was required. You can read more about it on http://blog.gingerlime.com/thumbs-up
Paul Robinson
Hi Yoav,
That’s why I love the open source world. Glad my post inspired you. Thanks for coming back & sharing the link too. 🙂