Static Caching web server proxy in php suitable for shared hosting installation

There are many reasons why you would want a static caching web proxy in php. It can improve the performance and scalability of your web site. It can help you have a static version of your web site.

For me it was that I had a huge web site with thousands of content pages in a cms that was no longer developed and I wanted to have a static copy of it for SEO purposes. After using it the site is much faster and I no longer need to use outdated software that can be a serious security risk. It was originally based on the ideas in this blog post, but instead of memcached I used static publishing.

The whole thing is two small files, and you only need to edit a few lines with the url of your site and its location on the disk. This can be installed on most shared hosting servers that give you php and have Apache with either the permission to use .htaccess files or edit apache conf.

.htaccess file

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /cacheproxy.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule . /cacheproxy.php [L,QSA]

cacheproxy.php file

class CacheProxy {

  private static $extarray=array(".jpg",".jpeg",".gif",".js",".css",".fla",".flv",".mpeg",".mpg"
  //edit this line and put the path of the directory this file is in
  private static $fsdir = "/home/mashy/public_html";
  //edit this file and put the domain name you want to proxy or path
  private static $old_host = '';
public static function mkpath($path)
    if(@mkdir($path) or file_exists($path)) return true;
    return (CacheProxy::mkpath(dirname($path)) and mkdir($path));

  private function __construct(){}

    public static function proxy(){
    $host = $_SERVER['HTTP_HOST'];    // We think we *are* this host, neato
    $host = self::$old_host;
    $path = rtrim(urldecode($_SERVER['REQUEST_URI']),"/");  // Grab the URI
    if ($path=="" or $path=="/")
        } else


            } else
    $url = "http://$host$path";
    header("Content-Type: ".get_headers($url,1)['Content-Type']);
    $data = self::get_url( $url );
    echo $data;



  /* fetch remote data */
  private static function get_url( $url ) {

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
    curl_setopt( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] );
    $file = curl_exec( $ch );
    curl_close( $ch );
    return $file;


// run the proxy for this request

Post Comment