homepage Welcome to WebmasterWorld Guest from 54.161.192.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Setting cache with htaccess
Cannot make it work!
Eugenios

10+ Year Member



 
Msg#: 1922 posted 9:10 pm on Aug 11, 2004 (gmt 0)

Hello,

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

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1922 posted 9:42 pm on Aug 11, 2004 (gmt 0)

> I also understand that using M in the ExpiresDefault I'm requesting that the page is cached in the server, not in the browser.

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"

> Also I wanted to know if this can make the bots pick cached pages instead of calling the script.

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

Eugenios

10+ Year Member



 
Msg#: 1922 posted 11:16 pm on Aug 11, 2004 (gmt 0)

Thanks Jim!

So as far as I understood you, I have to use a software (script) based cache system to have my pages cached for everybody.

Can you point me any software or script to do so?

Thanks in advance,

Enrique

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 1922 posted 2:45 am on Aug 12, 2004 (gmt 0)

> I have to use a software (script) based cache system to have my pages cached for everybody.

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:

  • Check for existence of requested page
  • If it exists, serve it
  • If not regenerate it by calling your original dynamic page generation script

    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]

    and the only change needed to your existing script is to save its output to a file called "page_name.html" in addition to sending it back to the client as it does now. You would also need to come up with a plan to delete these "cached" pages periodically or whenever their contents are updated, in order to let the script generate the new version.

    The above code is meant only as an example only, so please take it as such.

    Jim

  • Eugenios

    10+ Year Member



     
    Msg#: 1922 posted 6:09 pm on Aug 12, 2004 (gmt 0)

    Many Thanks Jim, I will check that htaccess code.

    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

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / Apache Web Server
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved