Forum Moderators: phranque
I have a number of sites (about 50-60), many of them accessing Amazon AWS with a perl script and others accessing MySQL for static data (read only). One site also performs read and write to MySQL.
Perl scripts are consuming a lot of server resources and would like to cache (on the server side) these pages. I don't care much about data freshness in this case. When Google, msn and other bots cralw my sites, the pages take a long while to load.
I placed this code in the .htaccess file on one site:
ExpiresActive on
<Files foo.cgi>
ExpiresDefault M2240000
</Files>
As far as I understand, this will cache my foo.cgi pages for about 30-40 days.
I also understand that using M in the ExipresDefault I'm requesting that the page is cached in the server, not in the browser.
It worked smoothly, but when I place it on the .htaccess of other sites (in the same server) it won't work.
Just wanted to know what I'm doing wrong. Also I wanted to know if this can make the bots pick cached pages instead of calling the script.
Any input will be of great help
Enrique
No, it says to apply the expiration time using the time the file was last modified, rather than the time the file was last accessed (A2240000).
While mod_expires will set the expires header, it does not establish a cache policy. To do that, you'll need to use mod_headers with a command such as
Header set Cache-control: "public"
Neither of these directives will give you "automatic" server-side caching. To do that, you'll need the script itself (or another script) to check for a cached version. If one exists, serve it. If not, create it and then serve it. Robots don't have any special ability to use your site in any way different from a browser; They ask your server for a resource (file, page, image, etc.) by URL, and index whatever comes back.
Jim
To have your server serve a "cached" file if available, and call your script to generate a new file if a cached version is not already available, you have to use a script, yes. That script can be built into your existing one, or you can write another one. You could do it in .htaccess, PERL, PHP, or several other scripting languages.
It boils down to:
This could be as simple as something like this in .htaccess using mod_rewrite:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(page_name)\.html$ /script.php?page=$1 [L]
The above code is meant only as an example only, so please take it as such.
Jim
In the meanwhile I'm using this script:
<?php
$cachefile = "cache/".$title.".html";
if (file_exists($cachefile))
{ include ($cachefile);
exit();
}
ob_start();
?>
Page with html and database here
<?php
$buffer = ob_get_contents();
$fp = fopen($cachefile, 'w');
fwrite($fp, $buffer);
fclose($fp);
?>
It doesn't check modification events, but it is what I need. Perhaps later I will add an expiration date.
I have yet to find how to do this with a perl script. I will post on the Perl forum to get help.
Thanks again,
Enrique