1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mod_expires (ExpiresActive On) without mod_expires

Discussion in 'PHP' started by frankm, Jan 11, 2006.

  1. #1
    I just moved some sites to a new host, that doesn't support mod_expires; not sure why, I contacted them about it.
    anyways, I like images to be as cached as it possible can,
    to reduce bandwidth and to increase the speed of the site.

    My 'normal' configuration would then be something like this: (.htaccess)
    But this host returns a 500 Internal Server Error a ExpiresActive is not understood.

    After some puzzling, I create this php file and .htaccess and want to share it with you. It does exactly the same as above, the difference is that the headers are 'faked' by PHP instead of the apache server (Which will be more efficient ofcourse; so ignore my piece if you can use mod_expires)
    file: expires.php
    $file = $_SERVER["DOCUMENT_ROOT"] . $_GET["file"];
    if (!file_exists($file))
      Header("HTTP/1.1 404 Not Found");
      Header("Content-Type: text/plain");
      echo "404 not found, image not found...";
    $ext = strtolower(strrpos($file, "."));
    case "gif": $mimetype= "image/gif"; break;
    case "pjpeg": case "jpeg": case "jpg": $mimetype= "image/jpeg"; break;
    case "png": $mimetype = "image/png"; break;
    case "ico": $mimetype = "image/x-icon"; break;
    default: $mimetype = "image/gif";
    header("Content-type: " . $mimetype);
    header("Cache-Control: max-age=".$cachetime);
    $t = filemtime($_SERVER["SCRIPT_FILENAME"]);
    header("Expires: " . date("r", time() + $cachetime));
    header("Last-Modified: " . date("r", $t));
    header("Content-Length: " . filesize($file));
    $fp = fopen($file, "rb");
    while ($fp && !feof($fp))
      $buf = fread($fp, 10000);
      echo $buf;
    if ($fp)
    file: .htaccess
    RewriteEngine On
    RewriteRule ^(.*)\.gif$     expires.php?file=/img/$1.gif [L]
    Code (markup):

    put both files in your /img/ directory.
    No warrenties - if it works for you, cool; if not, sorry.
    frankm, Jan 11, 2006 IP
  2. frankm

    frankm Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    frankm, Jan 11, 2006 IP